Answers for "capitilize first word js"

4

javascript capitalize first letter of each word

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"));
Posted by: Guest on May-01-2020
1

capitalize first letter of every word javascript

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);
Posted by: Guest on November-22-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language