Skip to content

Instantly share code, notes, and snippets.

@edgvi10
Forked from codeguy/slugify.js
Last active August 25, 2020 17:39
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 edgvi10/2d1f025f914b94ae7e2bd7d9dc00ca53 to your computer and use it in GitHub Desktop.
Save edgvi10/2d1f025f914b94ae7e2bd7d9dc00ca53 to your computer and use it in GitHub Desktop.
Create slug from string in Javascript
String.prototype.slugify = function (separator = "-") {
return this
.toString()
.normalize('NFD') // split an accented letter in the base letter and the acent
.replace(/[\u0300-\u036f]/g, '') // remove all previously split accents
.toLowerCase()
.trim()
.replace(/[^a-z0-9 ]/g, '') // remove all chars not letters, numbers and spaces (to be replaced)
.replace(/\s+/g, separator);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment