简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM