Last active
July 22, 2020 12:28
-
-
Save noudadrichem/cb68c13d28b2e2c1e289fd1dbd36baff to your computer and use it in GitHub Desktop.
Escape invalid chars in string to create a slug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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