Skip to content

Instantly share code, notes, and snippets.

@zorca
Forked from mathewbyrne/slugify.js
Created December 27, 2019 18:01
Show Gist options
  • Select an option

  • Save zorca/16f4572ec1ef7d6cb0d7a4f2791044e6 to your computer and use it in GitHub Desktop.

Select an option

Save zorca/16f4572ec1ef7d6cb0d7a4f2791044e6 to your computer and use it in GitHub Desktop.
Javascript Slugify
function slugify(text)
{
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment