In this article, we will learn how to capitalize the first letter of each word in a string in javascript. There are many ways to capitalize the first letter of each word in a string in javascript. Here are some examples.
Example 1: Using replace()
Method.
// To Capitalize First Letter of Each Word in a String - Title Case
function capitalizeEachWord(str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
console.log(capitalizeEachWord('The quick brown fox jumps over the lazy dog'));
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
console.log(capitalizeEachWord('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG'))
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
console.log(capitalizeEachWord('the quick brown Fox Jumps over the lazy Dog'))
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
Example 2: Using For Loop
// To Capitalize First Letter of Each Word in a String - Title Case
function capitalizeEachWord(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(' ');
}
console.log(capitalizeEachWord('The quick brown fox jumps over the lazy dog'));
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
console.log(capitalizeEachWord('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG'))
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
console.log(capitalizeEachWord('the quick brown Fox Jumps over the lazy Dog'))
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
Example 3: In this example, split()
, map()
and join()
methods.
// To Capitalize First Letter of Each Word in a String - Title Case
function capitalizeEachWord(str){
return str.toLowerCase()
.split(' ')
.map((s) => s.charAt(0).toUpperCase() + s.substring(1))
.join(' ');
}
console.log(capitalizeEachWord('The quick brown fox jumps over the lazy dog'));
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
console.log(capitalizeEachWord('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG'))
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
console.log(capitalizeEachWord('the quick brown Fox Jumps over the lazy Dog'))
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
Example 4: Another example of a regex and replace()
method.
// To Capitalize First Letter of Each Word in a String - Title Case
String.prototype.capitalizeEachWord = function(){
return this.toLowerCase().replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
};
var str1='The quick brown fox jumps over the lazy dog';
console.log(str1.capitalizeEachWord());
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
var str2='THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG';
console.log(str2.capitalizeEachWord());
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
var str3='the quick brown Fox Jumps over the lazy Dog';
console.log(str3.capitalizeEachWord());
//it returns ==> The Quick Brown Fox Jumps Over The Lazy Dog
I hope this article will help you to understand how to capitalize the first letter of each word in a string in javascript.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments