Botto
Botto

Reputation: 1208

Javascript regex non-capturing groups are returned

When I run

var episode_pattern = /(?:EPISODE\:\s*\#)([0-9]*)/g; 
console.log(episode_pattern.exec("EPISODE:  #3"));  

I get back both the "EPISODE: #3" and the "3" in the matches.
However using the (?: I expected to only get "3" in the matches array.

Upvotes: 1

Views: 32

Answers (1)

Pointy
Pointy

Reputation: 413702

The first element (element 0) of the returned array is always the entire matched string. In other words, if you have no groups at all, or if all your groups are non-capturing, you get back element 0. If you add groups without otherwise changing the regex, you still get back the same overall match in element 0, and then the groups start at element 1.

Upvotes: 4

Related Questions