Skip to content

Instantly share code, notes, and snippets.



Last active Jul 22, 2020
What would you like to do?
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