I’m looking at otherway to solve the problem and i don’t understand the first solution code. especially this line.
str = str.replace(/([a-z])([A-Z])/g, "$1 $2");
in explanation, it says that this line puts a space before any encountered uppercase characters in the string str so that the spaces can be replaced by dashes later on.
but for me, the code line above just looks like groupping smaller case to $1 and uppercase to $2.
can someone please explain this to me
pls link to excercise by using ‘get help’ link
$2 are capture group references.
$1 references the
([a-z]) in the regex and
$2 references the
([A-Z]). This code will replace all instances where there is a lowercase letter immediately followed by an uppercase letter with the same lowercase letter captured, followed by a single space character, and then followed by the same uppercase letter captured.
So if the string
"abCdefGh" had the
replace method applied, then
"ab Cdef Gh" would be the replacement.
I understand this
I was confused about this one
but after looking at it carefully again, I saw there’s a space between $1 and $2, therefore a single space will be added between lower and upper case.