Skip to content

Instantly share code, notes, and snippets.

@norfair00
Forked from bentruyman/slug.js
Created November 19, 2017 22:55
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 norfair00/b3b1d251e8548796cbd35c011f84c62c to your computer and use it in GitHub Desktop.
Save norfair00/b3b1d251e8548796cbd35c011f84c62c to your computer and use it in GitHub Desktop.
JavaScript Slug Generator
// Generates a URL-friendly "slug" from a provided string.
// For example: "This Is Great!!!" transforms into "this-is-great"
function generateSlug (value) {
// 1) convert to lowercase
// 2) remove dashes and pluses
// 3) replace spaces with dashes
// 4) remove everything but alphanumeric characters and dashes
return value.toLowerCase().replace(/-+/g, '').replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment