Skip to content

Instantly share code, notes, and snippets.

@ricardoriogo
Created February 13, 2013 01:59
Show Gist options
  • Save ricardoriogo/4859307 to your computer and use it in GitHub Desktop.
Save ricardoriogo/4859307 to your computer and use it in GitHub Desktop.
Slugify: Remove acentos e caracteres não permitidos em urls, substitui espaços, underlines e caracteres não alfanuméricos por hífen.
String.prototype.slugify = function(){
return (this)
.replace(/[ÁÀÂÃÄ]/gi, 'a')
.replace(/[ÉÈÊË]/gi, 'e')
.replace(/[ÍÌÎÏ]/gi, 'i')
.replace(/[ÓÒÔÕÖ]/gi, 'o')
.replace(/[ÚÙÛÜ]/gi, 'u')
.replace(/[Ç]/gi, 'c')
.toLowerCase() // change everything to lowercase
.replace(/^\s+|\s+$/g, '') // trim leading and trailing spaces
.replace(/[_|\s]+/g, '-') // change all spaces and underscores to a hyphen
.replace(/[^a-z0-9-\/]+/g, '') // remove all non-alphanumeric characters except the hyphen and bar
.replace(/[-]+/g, '-') // replace multiple instances of the hyphen with a single instance
.replace(/^-+|-+$/g, '') // trim leading and trailing hyphens
;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment