Tell us what’s happening:
While the code works, I’d like to understand why the regex in parentheses (^|\s)
works. The ^
should include the first character and the \s
includes white space. Why does the \s
select the character following white space and the ^
selects the first character, not the character following the first character (character 2)?
Your code so far
function titleCase(str) {
return str.toLowerCase().replace(/(^|\s)\S/g, (w) => w.toUpperCase());
}
titleCase("I'm a little tea pot");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.1 Safari/605.1.15
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence/