Answered by : nishan-nilupul
//Altered, "capitalize all words of a string" (javascript by Grepper on Jul 22 2019) answer to compatible with TypeScript
var myString = 'abcd abcd';
capitalizeWords(text){ return text.replace(/(?:^|\s)\S/g,(res)=>{ return res.toUpperCase();})
};
console.log(capitalizeWords(myString));
//result = "Abcd Abcd"
Source : | Last Update : Wed, 25 Nov 20
Answered by : grieving-gharial-54afdxs4rdwl
const str = 'FIDEURAM VITA';
function capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
const caps = str.split(' ').map(capitalize).join(' ');
caps; // 'Fideuram Vita'
Source : | Last Update : Mon, 02 Aug 21
Answered by : luis
text.replace(/(^\w|\s\w)/g, m => m.toUpperCase());
// Explanation:
//
// ^\w : first character of the string
// | : or
// \s\w : first character after whitespace
// (^\w|\s\w) Capture the pattern.
// g Flag: Match all occurrences.
// Example usage:
// Create a reusable function:
const toTitleCase = str => str.replace(/(^\w|\s\w)/g, m => m.toUpperCase());
// Call the function:
const myStringInTitleCase = toTitleCase(myString);
Source : https://stackoverflow.com/questions/32589197/how-can-i-capitalize-the-first-letter-of-each-word-in-a-string-using-javascript | Last Update : Mon, 29 Nov 21
Answered by : ankur-prajapati
function titleCase(str) { var splitStr = str.toLowerCase().split(' '); for (var i = 0; i < splitStr.length; i++) { // You do not need to check if i is larger than splitStr length, as your for does that for you // Assign it back to the array splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); } // Directly return the joined string return splitStr.join(' ');
}
document.write(titleCase("I'm a little tea pot"));
Source : https://stackoverflow.com/questions/32589197/how-to-capitalize-the-first-letter-of-each-word-in-a-string-using-javascript/45620677#45620677 | Last Update : Fri, 08 May 20
Answered by : proud-pygmy-ijdnjtpog3qn
const titleCase = title => title .split(/ /g).map(word => `${word.substring(0,1).toUpperCase()}${word.substring(1)}`) .join(" ");
Source : https://stackoverflow.com/questions/32589197/how-can-i-capitalize-the-first-letter-of-each-word-in-a-string-using-javascript/45620677#45620677 | Last Update : Mon, 10 Aug 20
Answered by : grieving-gharial-54afdxs4rdwl
//Without function
const str = 'FIDEURAM VITA';
const caps = str.split(' ').map(str =>str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()).join(' ')
caps; // 'Fideuram Vita'
Source : | Last Update : Mon, 02 Aug 21
Answered by : godswill-ohiole-agangan
const word = "freecodecamp"
const capitalized = word.charAt(0).toUpperCase() + word.slice(1)
// Freecodecamp
// F is capitalized
Source : https://www.freecodecamp.org/news/javascript-capitalize-first-letter-of-word/ | Last Update : Fri, 01 Jul 22
Answered by : caio-guilherme
function ucwords(str,force){ str=force ? str.toLowerCase() : str; return str.replace(/(\b)([a-zA-Z])/g, function(firstLetter){ return firstLetter.toUpperCase(); });
}
Source : https://stackoverflow.com/questions/2017456/with-jquery-how-do-i-capitalize-the-first-letter-of-a-text-field-while-the-user | Last Update : Fri, 24 Jun 22