Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adriancmiranda/d21a4b84c6c88a3b4e38c11ad73e7403 to your computer and use it in GitHub Desktop.
Save adriancmiranda/d21a4b84c6c88a3b4e38c11ad73e7403 to your computer and use it in GitHub Desktop.
An Javascript function to remove accents, spaces and set lower case from an input string.
function slugify (str) {
var map = {
'-' : ' ',
'-' : '_',
'a' : 'á|à|ã|â|À|Á|Ã|Â',
'e' : 'é|è|ê|É|È|Ê',
'i' : 'í|ì|î|Í|Ì|Î',
'o' : 'ó|ò|ô|õ|Ó|Ò|Ô|Õ',
'u' : 'ú|ù|û|ü|Ú|Ù|Û|Ü',
'c' : 'ç|Ç',
'n' : 'ñ|Ñ'
};
str = str.toLowerCase();
for (var pattern in map) {
str = str.replace(new RegExp(map[pattern], 'g'), pattern);
};
return str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment