Skip to content

Instantly share code, notes, and snippets.

@panphora
Created April 1, 2019 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panphora/809cd487a49ee0f7029ea14a0c8ccfc9 to your computer and use it in GitHub Desktop.
Save panphora/809cd487a49ee0f7029ea14a0c8ccfc9 to your computer and use it in GitHub Desktop.
export default function slugify (str) {
return str
.trim()
.replace(/[^0-9A-Za-z\-\s]/g, " ") // keep alphanumeric, dashes, and spaces
.replace(/ +/g, " ") // replace double spaces with single space
.replace(/ /g, "-") // replace single spaces with single dash
.replace(/\-\-/g, "-") // replace double dashes with single dash
.toLowerCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment