Created
October 30, 2018 12:58
-
-
Save neps-in/25e18891996e4682c6c0d70989bb900e to your computer and use it in GitHub Desktop.
Slugifies the text in pure javascript
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
| /* | |
| Slugifies the text in pure javascript | |
| Converts | |
| What's the best way to learn PHP ? => whats-the-best-way-to-learn-php | |
| */ | |
| function slugify(text) | |
| { | |
| 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 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment