How to useRegular Expressions and String Split in Javascript
In this approach, we utilize regular expressions along with the split() method to split the string into words, then we capitalize the first letter of each word, and finally, we join the words back together to form the title case string.
function titleCase(str) {
return str.toLowerCase().replace(/(?:^|\s)\w/g, function(match) {
return match.toUpperCase();
});
}
console.log(titleCase("converting string to titlecase"));
Output
Converting String To Titlecase
Convert string to title case in JavaScript
Converting string to title case means converting to uppercase the first letter of every word in a sentence while the other ones remain in lowercase. In this article, we will see how to convert string to title case in JavaScript
Below are the approaches to convert string to title case in JavaScript:
Table of Content
- Using replace() function
- Using For loop to titlecase a string
- Using map() method
- Using reduce() method
- Using Foreach loop
- Using Regular Expressions and String Split
- Using Lodash’s _.startCase
Contact Us