- Instant help with your JavaScript coding problems

Capitalize words in a string using JavaScript

Question:
How to capitalize words in a string using JavaScript?
Answer:
function capitalizeWords(str) {
    return str.replace(/\b\w/g, l => l.toUpperCase())
}

console.log(capitalizeWords('this is my text'));
Description:

To capitalize words in a string without any external library can be done with a simple string replace function using a basic regexp. 

Share "How to capitalize words in a string using JavaScript?"
Related snippets:
Tags:
capitalize, words, all, string, convert, javascript
Technical term:
Capitalize words in a string using JavaScript