-
-
Save blizzrdof77/5260b396674aa7d30b12e9fc7786e695 to your computer and use it in GitHub Desktop.
Javascript Slugify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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