Last active
October 12, 2018 16:21
-
-
Save niketpathak/ba9bb2d551efb9ab867048f95ae0265d to your computer and use it in GitHub Desktop.
Generate slug using Javascript
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
/** | |
* Generate a slug | |
* @param inputString The input String | |
* @returns {string} | |
*/ | |
function slugify(inputString) { | |
return inputString.toString().toLowerCase().trim() | |
.replace(/&/g, '-and-') // Replace & with 'and' | |
.replace(/[\s\W-]+/g, '-') // Replace spaces, non-word characters and multiple-dashes with a single dash (-) | |
.replace(/(^-|-$)/g, '') // Remove dangling hypens in case slug begins or ends with a special character | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment