Created
March 6, 2012 18:16
-
-
Save adamjforster/1987870 to your computer and use it in GitHub Desktop.
A function to slugify a string in JavaScript.
This file contains 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
function slugify(value) { | |
/** | |
* Convert to lowercase, remove non-alphanumeric characters and replace | |
* whitespace with hyphens. | |
* | |
* Inspired by Django's slugify function. | |
*/ | |
value = value.toLowerCase().trim(); | |
value = value.replace(/[^\w\s\-]/g, ''); | |
return value.replace(/[\s\-]+/g, '-'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment