I don't understand this regex

What does this /(?:underscore| )+/ regex match? Isn’t “?:” for non-capturing?
Why does this split the string at each “underscore” if it is not to be matched?

str.split(/(?:_| )+/)

function spinalCase(str) {
str = str.replace(/([a-z])([A-Z])/g, "$1 $2");

str = str.toLowerCase();

str = str.split(/(?:_| )/g); 

str = str.join("-");


  **Your browser information:**

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

Challenge: Spinal Tap Case

Link to the challenge:

Either an underscore or a space.


You can easily check this by trying the code without the ?: to see what happens. Here’s an example:

We do not want the _ to be in our array.


Okay so when you use ?:\_ in .split() it omits all underscores when splitting into the substring array?

Right. It matches it without capturing it.

1 Like

Great, thanks for your help. That makes a lot more sense now.