Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gogones/babfa2dabb7b8488f849a1f815cec946 to your computer and use it in GitHub Desktop.
Save gogones/babfa2dabb7b8488f849a1f815cec946 to your computer and use it in GitHub Desktop.
Javascript / ES6 - Uppercase first letter of each word (2 ways) -- via: https://stackoverflow.com/a/4878800
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
// or in ES6:
var text = "foo bar loo zoo moo";
const ucfirst = text => text.toLowerCase()
.split(' ')
.map((s) => s.charAt(0).toUpperCase() + s.substring(1))
.join(' ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment