Skip to content

Instantly share code, notes, and snippets.

@niketpathak
Last active October 12, 2018 16:21
Show Gist options
  • Save niketpathak/ba9bb2d551efb9ab867048f95ae0265d to your computer and use it in GitHub Desktop.
Save niketpathak/ba9bb2d551efb9ab867048f95ae0265d to your computer and use it in GitHub Desktop.
Generate slug using Javascript
/**
* 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