Skip to content

Instantly share code, notes, and snippets.

@neps-in
Created October 30, 2018 12:58
Show Gist options
  • Select an option

  • Save neps-in/25e18891996e4682c6c0d70989bb900e to your computer and use it in GitHub Desktop.

Select an option

Save neps-in/25e18891996e4682c6c0d70989bb900e to your computer and use it in GitHub Desktop.
Slugifies the text in pure javascript
/*
Slugifies the text in pure javascript
Converts
What's the best way to learn PHP ? => whats-the-best-way-to-learn-php
*/
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