Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leandrodasilvaalves/408840338523f10ba6ef22d38e4bedc4 to your computer and use it in GitHub Desktop.
Save leandrodasilvaalves/408840338523f10ba6ef22d38e4bedc4 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': 'ñ|Ñ',
'': '´|`|"|!|@|!|#|\\$|%|¨|&|\\(|\\)|\\=|\\+|\\{|\\}|\\[|\\]|ª|º|~|\\^|<|>|,|\\.|;|:|\\?|/|°|\\*|\\||\\-|_',
};
for (var pattern in map) {
str = str.replace(new RegExp(map[pattern], 'g'), pattern);
};
return str.replaceAll(" ", "-").toLowerCase();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment