Skip to content

Instantly share code, notes, and snippets.

@sholsinger
Created May 2, 2011 16:18
Show Gist options
  • Save sholsinger/951859 to your computer and use it in GitHub Desktop.
Save sholsinger/951859 to your computer and use it in GitHub Desktop.
JavaScript method to convert a string into a reasonable "key" string.
if(String.prototype.toKey === undefined)
{
String.prototype.toKey = function(){
var s = this.toLowerCase();
s = s.replace(/^[0-9_-]+|[^a-z0-9_\s-]|(\s.\s){1,}|[\s]{2,}|[_\s-]+$/ig, '');
s = s.replace(/[\-\s_]+/ig,"_");
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment