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};
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'
1const str = 'captain picard';
2
3function capitalize(str) {
4 return str.charAt(0).toUpperCase() + str.slice(1);
5}
6
7const caps = str.split(' ').map(capitalize).join(' ');
8caps; // 'Captain Picard'
1const toTitleCase = (phrase) => {
2 return phrase
3 .toLowerCase()
4 .split(' ')
5 .map(word => word.charAt(0).toUpperCase() + word.slice(1))
6 .join(' ');
7};
8
9let result = toTitleCase('maRy hAd a lIttLe LaMb');
10console.log(result);
1const titleCase = function(text) {
2 let newText = '';
3 text = text.toLowerCase();
4 text = text.charAt(0).toUpperCase() + text.slice(1);
5 for (let i = 0; i < text.length; i++) {
6 if (text[i] === ' ') {
7 newText += ' ' + text[i+1].toUpperCase();
8 i++;
9 } else {
10 newText += text[i];
11 }
12 }
13 return newText;
14}