Skip to content

Instantly share code, notes, and snippets.

@sophieelsa
Forked from merolhack/slugify.js
Created May 14, 2019 09:59
Show Gist options
  • Save sophieelsa/84831f841f258b5c69569a3f3d715d23 to your computer and use it in GitHub Desktop.
Save sophieelsa/84831f841f258b5c69569a3f3d715d23 to your computer and use it in GitHub Desktop.
Javascript Slugify: For accents and other latin characters
function slugify(text)
{
var from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;";
var to = "aaaaaeeeeeiiiiooooouuuunc------";
_.each( from, function( character, i ) {
text = text.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
});
return text
.toString() // Cast to string
.toLowerCase() // Convert the string to lowercase letters
.trim() // Remove whitespace from both sides of a string
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/&/g, '-y-') // Replace & with 'and'
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-'); // Replace multiple - with single -
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment