Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save quangquy87/ecb94d4437f9c6ab7dbfc07cc88afbae to your computer and use it in GitHub Desktop.
Save quangquy87/ecb94d4437f9c6ab7dbfc07cc88afbae to your computer and use it in GitHub Desktop.
String Functions for Javascript – trim, to camel case, to dashed, and to underscore
Trim String
String.prototype.trim = function(){
return this.replace(/^\s+|\s+$/g, "");
};
To Camel Case
String.prototype.toCamel = function(){
return this.replace(/(\-[a-z])/g, function($1){return $1.toUpperCase().replace('-','');});
};
To Dashed from Camel Case
String.prototype.toDash = function(){
return this.replace(/([A-Z])/g, function($1){return "-"+$1.toLowerCase();});
};
To Underscore from Camel Case
String.prototype.toUnderscore = function(){
return this.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment