Skip to content

Instantly share code, notes, and snippets.

@noudadrichem
Last active July 22, 2020 12:28
Show Gist options
  • Select an option

  • Save noudadrichem/cb68c13d28b2e2c1e289fd1dbd36baff to your computer and use it in GitHub Desktop.

Select an option

Save noudadrichem/cb68c13d28b2e2c1e289fd1dbd36baff to your computer and use it in GitHub Desktop.
Escape invalid chars in string to create a slug
const toSlug = 'every thing you $ want. To b3 eScaped';
const slug = toSlug.toLowerCase()
.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment