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("-");

};

spinalCase('The_Andy_Griffith_Show');
  **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.

Yes.

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

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

2 Likes

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.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.