Passed but would like feedback

Tell us what’s happening:
I finally passed this one after a lot of fiddling with the second regex. When I finish a challenge, I try to look at all of the solutions listed and learn how I could have made my function more simple and streamlined. I understand how the final solution in the list works, but I am struggling with understanding "$1 $2" in the first and second solutions. I did some researching and it seems like $1 is telling it to put a space before each encountered upper case character, but how? I would be incredibly grateful if someone could break down for me what is happening.

**Solution 2 code **

function spinalCase(str) {
  // Replace low-upper case to low-space-uppercase
  str = str.replace(/([a-z])([A-Z])/g, "$1 $2");
  // Split on whitespace and underscores and join with dash
  return str
    .split(/(?:_| )+/)

// test here
spinalCase("This Is Spinal Tap");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36.

Challenge: Spinal Tap Case

Link to the challenge:

the $1 refers to the first capture group in the regex pattern, the $2 to the second capture group
"$1 $2" says to put what was captured by the two groups in the end string separated by a space

1 Like

okay so any lowercase character followed by any uppercase character will be separated by a space?

yes, the regex match a lower case character followed by an upper case character, and the capture groups say what is referenced with the number

1 Like