Skip to content

Instantly share code, notes, and snippets.

@BBB
Created May 31, 2011 14:56
Show Gist options
  • Save BBB/1000644 to your computer and use it in GitHub Desktop.
Save BBB/1000644 to your computer and use it in GitHub Desktop.
Will convert any string into safe slug. (Replaces any invalid characters with hypens, removes 1+ hypens & removes hyphens from the start & end of string)
String.prototype.slugify = function () {
return this.replace(/\s/g,'-').replace(/[^a-zA-Z0-9\-]/g,'').replace(/[\-]+/g,'-').replace(/\-$/,'').replace(/^\-/,'').toLowerCase();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment