DEV Community

Discussion on: Web Basics: How to Capitalize a Word in JavaScript

 
diek profile image
diek

Hi, the i doesn't does anything in this case, it is my mania of writing it always. The mdn's docu says this about \w:

Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_]

So \w is case insesitive already.

Thread Thread
 
qm3ster profile image
Mihail Malo • Edited

OH NO

'mi ke$ha'.toLowerCase().replace(/\b\w/gi, c => c.toUpperCase())
//> "Mi Ke$Ha"

:'(

Thread Thread
 
diek profile image
diek

That was expected, which result do you wanted?

Thread Thread
 
qm3ster profile image
Mihail Malo

Try my honestly unironically inferior regex and see:

'mi ke$ha'.toLowerCase().replace(/(?:^|\s)\w/g, c => c.toUpperCase())