Skip to content

Instantly share code, notes, and snippets.

@joemidi
Created May 19, 2020 14:15
Show Gist options
  • Save joemidi/0dc961335a138248ab0d739aa3f65480 to your computer and use it in GitHub Desktop.
Save joemidi/0dc961335a138248ab0d739aa3f65480 to your computer and use it in GitHub Desktop.
function slugify(text)
{
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/(\.png|\.jpg|\.jpeg)/, '') // Remove file exenstion
.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