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
.toLowerCase()
.split(/(?:_| )+/)
.join("-");
}
// 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: