Spinal Case Help

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


  **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:

the two groups match two neighbouring letters, the first group match one letter, the second group match one letter, with nothing in between

1 Like

It finds where a lower case letter is next to an upper case letter.

Separates the string in-between the two.

'$1 $2'

'$1 would be "s" and $2" would be "I" and the space in-between is us adding the space in-between those 2 characters in the string, yea?