Skip to content

Instantly share code, notes, and snippets.

@sajidzaman
Forked from mathewbyrne/slugify.js
Created October 3, 2017 11:07
Show Gist options
  • Save sajidzaman/05341afb1c2f8ac1f15018e2522cf8a8 to your computer and use it in GitHub Desktop.
Save sajidzaman/05341afb1c2f8ac1f15018e2522cf8a8 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