Skip to content

Instantly share code, notes, and snippets.

@spences10
Last active March 30, 2018 12:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spences10/27ee6e716ba31de495987bec356a2aae to your computer and use it in GitHub Desktop.
Save spences10/27ee6e716ba31de495987bec356a2aae to your computer and use it in GitHub Desktop.
Slug helper!
export const slugIt = text => {
if (!text) return
return text
.toString()
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
}
console.log(slugIt('!" NO CAPS or funky symbols !£%£$%^@: hello£$%^ world!'));
// no-caps-or-funky-symbols-hello-world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment