Last active
August 30, 2017 09:34
-
-
Save jacobovidal/8b779df48abfd7d3634d3516a70f9ed3 to your computer and use it in GitHub Desktop.
JavaScript slugify
This file contains hidden or 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
function slugify(text) | |
{ | |
var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž'; | |
var accentsOut = "AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz"; | |
text = text.split(''); | |
var txtLen = text.length; | |
var i, x; | |
for (i = 0; i < txtLen; i++) { | |
if ((x = accents.indexOf(text[i])) != -1) { | |
text[i] = accentsOut[x]; | |
} | |
} | |
var text = text.join(''); | |
return text.toString().toLowerCase() | |
.replace(/\s+/g, '-') // Replace spaces with - | |
.replace(/[^\w\-]+/g, '') // Remove all non-word chars | |
.replace(/\-\-+/g, '-') // Replace multiple - with single - | |
.replace(/^-+/, '') // Trim - from start of text | |
.replace(/-+$/, ''); // Trim - from end of text | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment