Skip to content

Instantly share code, notes, and snippets.

@octopitus
Forked from juanmhidalgo/js-toSlug.js
Created September 20, 2016 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octopitus/56a189e379377787d7366e5048095a1b to your computer and use it in GitHub Desktop.
Save octopitus/56a189e379377787d7366e5048095a1b to your computer and use it in GitHub Desktop.
JavaScript toSlug()
String.prototype.toSlug = function(){
st = this.toLowerCase();
st = st.replace(/[\u00C0-\u00C5]/ig,'a')
st = st.replace(/[\u00C8-\u00CB]/ig,'e')
st = st.replace(/[\u00CC-\u00CF]/ig,'i')
st = st.replace(/[\u00D2-\u00D6]/ig,'o')
st = st.replace(/[\u00D9-\u00DC]/ig,'u')
st = st.replace(/[\u00D1]/ig,'n')
st = st.replace(/[^a-z0-9 ]+/gi,'')
st = st.trim().replace(/ /g,'-');
st = st.replace(/[\-]{2}/g,'');
return (st.replace(/[^a-z\- ]*/gi,''));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment