[Javascript] trim, to camel case, to dashed, and to underscore

Trim String

1
2
3
String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g, "");
};

To Camel Case

1
2
3
String.prototype.toCamel = function(){
	return this.replace(/(\-[a-z])/g, function($1){return $1.toUpperCase().replace('-','');});
};

To Dashed from Camel Case

1
2
3
String.prototype.toDash = function(){
	return this.replace(/([A-Z])/g, function($1){return "-"+$1.toLowerCase();});
};

To Underscore from Camel Case

1
2
3
String.prototype.toUnderscore = function(){
	return this.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
};


출처 : https://jamesroberts.name/blog/2010/02/22/string-functions-for-javascript-trim-to-camel-case-to-dashed-and-to-underscore/