Skip to content

Instantly share code, notes, and snippets.

@jacobovidal
Last active August 30, 2017 09:34
Show Gist options
  • Save jacobovidal/8b779df48abfd7d3634d3516a70f9ed3 to your computer and use it in GitHub Desktop.
Save jacobovidal/8b779df48abfd7d3634d3516a70f9ed3 to your computer and use it in GitHub Desktop.
JavaScript slugify
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