Skip to content

Instantly share code, notes, and snippets.

@truetone
Forked from bentruyman/slug.js
Last active August 29, 2015 14:06
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 truetone/3739a45ca238c2f2aea2 to your computer and use it in GitHub Desktop.
Save truetone/3739a45ca238c2f2aea2 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) replace anything that's not an alphanumeric character or dash with a dash
// 3) replace instances of more than one dash with a single dash
// 4) remove leading and trailing dashes
return value.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, '');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment