Skip to content

Instantly share code, notes, and snippets.

@chrisma
Last active August 29, 2015 14:08
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 chrisma/d052756be4ac383d0793 to your computer and use it in GitHub Desktop.
Save chrisma/d052756be4ac383d0793 to your computer and use it in GitHub Desktop.
Javascript String helpers
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
String.prototype.title = function(force) {
//'examPLE'.title(); -> 'ExamPLE'
//'examPLE'.title(true); -> 'Example'
return (force ? this.toLowerCase() : this).replace(/(?:^|\s)\S/g, function(s) { return s.toUpperCase(); });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment