Created
February 13, 2013 01:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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