Last updated: July 11, 2021
Capitalizes the first letter of every word in a passed string.
const capitalizeEachWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());
pattern and the
pattern can be a string or a RegExp, and the
replacement can be a string or a function to be called for each match. We passed the Regular Expressions as a pattern where it looks for lowercase words matches. Next is we used a function as the replacement which then converts each word matches to uppercase.
str - the word or sentence in a string format
Returns a string of capitalized first letter of every word in a sentence
You can pass a word:
capitalizeEachWord('ohayō'); // Result: "Ohayō"
You can pass a sentence:
capitalizeEachWord('hello, luigi!'); // Result: "Hello, Luigi!"