Skip to content

Instantly share code, notes, and snippets.

@ryanhs
Forked from rickycheers/ucwords.js
Created May 22, 2016 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ryanhs/53dc2be0ee5129a01013395fb2917a34 to your computer and use it in GitHub Desktop.
Save ryanhs/53dc2be0ee5129a01013395fb2917a34 to your computer and use it in GitHub Desktop.
ucwords in JavaScript - Equivalent to PHP's ucwords() Returns a string with the first letter in upper case of each word.
String.prototype.ucwords = function() {
str = this.toLowerCase();
return str.replace(/(^([a-zA-Z\p{M}]))|([ -][a-zA-Z\p{M}])/g,
function(s){
return s.toUpperCase();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment