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.
$1 and $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.