Skip to content

Instantly share code, notes, and snippets.

@adamjforster
Created March 6, 2012 18:16
Show Gist options
  • Save adamjforster/1987870 to your computer and use it in GitHub Desktop.
Save adamjforster/1987870 to your computer and use it in GitHub Desktop.
A function to slugify a string in JavaScript.
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