Forked from whoisryosuke/gist:5b0d54926c997a6620945d780958ea74
Created
November 9, 2021 09:24
-
-
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
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
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