Skip to content

Instantly share code, notes, and snippets.

@zabaala
Created July 14, 2016 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zabaala/b8d0e9fe654c5d4abf86359fa83c1de8 to your computer and use it in GitHub Desktop.
Save zabaala/b8d0e9fe654c5d4abf86359fa83c1de8 to your computer and use it in GitHub Desktop.
function slugify(str) {
str = str.toString().toLowerCase().trim()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-'); // Replace multiple - with single -
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment