I'm unsure as to how the whitespace section works. This snippet comes from freecodecamp and it's given as a solution to a problem where you're supposed to make every first letter upper case.
Here is the full algorithm:
function titleCase(str) {
return str
.toLowerCase()
.replace(/(^|\s)\S/g, L => L.toUpperCase());
}
And their explanation for the regex:
Regex explanation:
Find all non-whitespace characters (\S)
At the beginning of string (^)
Or after any whitespace character (\s)
I don't understand why \s selects characters after whitespace characters. From what I understand \s just selects whitespace characters. Is it because of the (^) at the start of the OR grouping? If so, why?