Tell us what’s happening:
Hi,
Can you please explain the below regular expressions because I don’t understand them.
str = str.replace(/([a-z])([A-Z])/g, '$1 $2');
return str.toLowerCase().split(/(?:_| )+/) .join('-');
Thanks,
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case
Hello,
In the first line, we are looking for any lowercase characters [a-z]
followed by any uppercase characters [A-Z]
, the ()
are there to captures the matches, ie what would be interpreted as the $1
and $2
in the second argument for replace
, in this case we’re adding a space between them.
In the second line were are splitting the string whenever we encounter an underscore (_
) or (|
) a space, one or more times (+
), and joining the splits with a dash (-
) effectively replacing the matches with -
s.
the (?: )
part is a non capturing group, because we don’t need the matches like in he first line.
Thanks a lot for your very detailed reply. I have understood everything except the ($1 $2) and the (?: ).
you’re welcome, sorry for the late reply. you can think of them as placeholders. Hope that helps.