I looked at some questions and answers about capitalize in StackOverflow but could not find answer about my problem.
I would like to capitalize first letter of each word in a string only if word lengh > 2.
My temporary solution was:
var str = str.toLowerCase().replace(/\b[a-z]/g, function (letter) {
return letter.toUpperCase();
}).replace(/\b\w{1,2}\b/g, function (letter) {
return letter.toLowerCase();
});
There is a solution that can unite the two regex in one?
Here's a simpler solution which does the job.
const capitalize = (str) =>
str.toLowerCase().replace(/\w{3,}/g, (match) =>
match.replace(/\w/, (m) => m.toUpperCase()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With