I am having trouble understanding how exactly the capture groups are separating the words, capture group one /([a-z])/
selects a lower case character from a-z, the second the same but for Upper case /([A-Z])/
.
You cannot switch them or it doesn’t work, but I dont understand what’s going on, my guess was, its searches for lowercase a - z until it comes to an uppercase, but that doesn’t make any sense because it would only return the uppercase letter for group 2 since the next letter is usually a lower case?
I’ve looked through comments, solutions, and MDN webdocs, but nothing seems to explain what exactly is going on in the code, in a way that I understand anyways. I am also really bad at regular expressions. Definitely my worst suit in js so far.
The Question:
OR is it, it searches for any character such as t and then when the second group finally gets a capital letter it says everything before this capital letter is a group?
function spinalCase(str) {
let ex = /\s|_/g
let newStr = str.replace(/([a-z])([A-Z])/g, '$1 $2')
newStr = newStr.replace(ex, "-")
return newStr
}
spinalCase("thisIsSpinalTap");
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0
Challenge: Spinal Tap Case
Link to the challenge: