Created
February 18, 2012 05:55
-
-
Save peelman/1857716 to your computer and use it in GitHub Desktop.
Useful JS String Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from: http://jamesroberts.name/blog/2010/02/22/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, ""); | |
}; | |
//Camel Case | |
String.prototype.toCamel = function(){ | |
return this.replace(/(\-[a-z])/g, function($1){return $1.toUpperCase().replace('-','');}); | |
}; | |
// Dashed from Camel | |
String.prototype.toDash = function(){ | |
return this.replace(/([A-Z])/g, function($1){return "-"+$1.toLowerCase();}); | |
}; | |
// Underscored from Camel | |
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