1//capitalize only the first letter of the string.
2function capitalizeFirstLetter(string) {
3 return string.charAt(0).toUpperCase() + string.slice(1);
4}
5//capitalize all words of a string.
6function capitalizeWords(string) {
7 return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
8};
1function capitalizeFirstLetter(string) {
2 return string.charAt(0).toUpperCase() + string.slice(1);
3}
4
5console.log(capitalizeFirstLetter('foo bar bag')); // Foo
1const lower = 'this is an entirely lowercase string';
2const upper = lower.charAt(0).toUpperCase() + lower.substring(1);
1const uppercaseWords = str => str.replace(/^(.)|\s+(.)/g, c => c.toUpperCase());
2
3// Example
4uppercaseWords('hello world'); // 'Hello World'
1function capitalizeFirstLetter(string) {
2 return string.charAt(0).toUpperCase() + string.slice(1);
3}
4
5console.log(capitalizeFirstLetter('foo')); // Foo
1function ucwords (str) {
2 return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
3 return $1.toUpperCase();
4 });
5}
6
7console.log(ucwords("hello world"));