Skip to content

Instantly share code, notes, and snippets.

@blizzrdof77
Forked from mathewbyrne/slugify.js
Last active December 6, 2018 00:13
Show Gist options
  • Save blizzrdof77/5260b396674aa7d30b12e9fc7786e695 to your computer and use it in GitHub Desktop.
Save blizzrdof77/5260b396674aa7d30b12e9fc7786e695 to your computer and use it in GitHub Desktop.
Javascript Slugify
/**
* Slugify
*
* @param string text
* @return string
*/
function cleanSlug(text) {
return (
text.toString().toLowerCase()
.replace(/\s+/g, '-') /* Replace spaces with - */
.replace(/[^\w\-]+/g, '') /* Remove all non-word chars */
.replace(/\-\-+/g, '-') /* Replace multiple '--' with '-' */
.replace(/^-+/, '').replace(/-+$/, '') /* Trim text */
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment