text.replace(/(^w|sw)/g,m=> m.toUpperCase());// Explanation:// // ^w : first character of the string// | : or// sw : first character after whitespace// (^w|sw) Capture the pattern.// g Flag: Match all occurrences.// Example usage:// Create a reusable function:consttoTitleCase=str=> str.replace(/(^w|sw)/g,m=> m.toUpperCase());// Call the function:const myStringInTitleCase =toTitleCase(myString);
consttoTitleCase=(phrase)=>{return phrase
.toLowerCase().split(' ').map(word=> word.charAt(0).toUpperCase()+ word.slice(1)).join(' ');};let result =toTitleCase('maRy hAd a lIttLe LaMb');console.log(result);
//Altered, "capitalize all words of a string" (javascript by Grepper on Jul 22 2019) answer to compatible with TypeScriptvar myString ='abcd abcd';capitalizeWords(text){return text.replace(/(?:^|s)S/g,(res)=>{return res.toUpperCase();})};console.log(capitalizeWords(myString));//result = "Abcd Abcd"
constcapitalize=(s)=>{if(typeof s !=='string')return''return s.charAt(0).toUpperCase()+ s.slice(1)}capitalize('flavio')//'Flavio'capitalize('f')//'F'capitalize(0)//''capitalize({})//''
functiontitleCase(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 stringreturn splitStr.join(' ');}document.write(titleCase("I'm a little tea pot"));
const names =["alice","bob","charlie","danielle"]// --> ["Alice", "Bob", "Charlie", "Danielle"]//Just use some anonymous function and iterate through each of the elements in the array//and take string as another arraylet namescap = names.map((x)=>{return x[0].toUpperCase()+x.slice(1)})console.log(namescap)
first letter of each word in a sentence to uppercase javascript
functioncapitalizeTheFirstLetterOfEachWord(words){var separateWord = words.toLowerCase().split(' ');for(var i =0; i < separateWord.length; i++){
separateWord[i]= separateWord[i].charAt(0).toUpperCase()+
separateWord[i].substring(1);}return separateWord.join(' ');}console.log(capitalizeTheFirstLetterOfEachWord("my name is john"));
// this will only capitalize the first wordvar name =prompt("What is your name");
firstLetterUpper = name.slice(0,1).toUpperCase();alert("Hello "+ firstLetterUpper + name.slice(1, name.length).toLowerCase());
// includeAllCaps is optional and defaults to false// if includeAllCaps is set to true, it will Title Case words with all capital letters// includeMinorWords is optional and defaults to false// if includeMinorWords is set to true, it will minor words in the middle of the stringfunctiontoTitleCase(str, includeAllCaps, includeMinorWords){
includeAllCaps =(includeAllCaps ?(includeAllCaps ==true?true:false):false);
includeMinorWords =(includeMinorWords ?(includeMinorWords ==true?true:false):false);var i, j, lowers;
str = str.replace(/([^W_]+[^s-]*) */g,function(txt){if(!/[a-z]/.test(txt)&&/[A-Z]/.test(txt)&&!includeAllCaps){return txt;}else{return txt.charAt(0).toUpperCase()+ txt.substr(1).toLowerCase();}});if(includeMinorWords){return str;}else{// Certain minor words should be left lowercase unless // they are the first or last words in the string
lowers =['A','An','The','And','But','Or','For','Nor','As','At','By','For','From','In','Into','Near','Of','On','Onto','To','With'];for(i =0, j = lowers.length; i < j; i++)
str = str.replace(newRegExp('s'+ lowers[i]+'s','g'),function(txt){return txt.toLowerCase();});return str;}}toTitleCase("FOO bar");// FOO BartoTitleCase("FOO bar",true);// Foo BartoTitleCase("a foo bar");// A Foo BartoTitleCase("a foo in bar");// A Foo in BartoTitleCase("foo of bar");// Foo of BartoTitleCase("foo of bar",false,true);// Foo Of Bar