Tell us what’s happening:
Hello there
I’m having troubles understanding the regex here.
Your code so far
function spinalCase(str) {
return str.trim().split(/\W|_|(?=[A-Z])/).join('-').toLowerCase();
}
spinalCase("thisIsSpinalTap");
I was doing something similar on my own, but I had to look the solution for the (?=[A-Z])
The thing is when I use the regex to split a string like .split(/\W|_/)
, it cut the string when it find the character by removing the character, so in our final string there is no space (or _) left.
So when we put (?=[A-Z]) which I think means “find a character where the next character is a capital letter”. It should for example indicate s,s,l in thisIsSpinalTap
, to transform it to something like thi-i-spina-tap
Yet by some magic this one doesn’t delete any character. How lucky, we didn’t want that, but still I don’t understand the reason?
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case