Skip to content

Instantly share code, notes, and snippets.

@loickreitmann
Created April 14, 2011 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loickreitmann/920816 to your computer and use it in GitHub Desktop.
Save loickreitmann/920816 to your computer and use it in GitHub Desktop.
Converts string to underscored.
/**
* toUnderscored: converts string to underscored
*/
String.prototype.toUnderscored = function () {
return this.replace(/^\s+|\s+$/g, "")
.toLowerCase()
.replace(/_/g, '')
.replace(/(\s[a-z0-9])/g, function ($1) {
return $1.replace(/\s/, '_');
})
.replace(/[^a-z0-9_]/g, '');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment