简体   繁体   中英

Does omitting non-capturing groups ?: have a negative effect on RegExp.prototype.test()?

I am currently testing a string to be in a specific mail format.

/^[A-Za-z0-9](([A-Za-z0-9]|\.(?!\.))*[A-Za-z0-9]+)?@[A-Za-z0-9]+\.[A-Za-z]{2,}$/.test(email)

Now I have some parenthesis which could be marked as non-capturing groups like this:

/^[A-Za-z0-9](?:(?:[A-Za-z0-9]|\.(?!\.))*[A-Za-z0-9]+)?@[A-Za-z0-9]+\.[A-Za-z]{2,}$/.test(email)

But I don't see any difference between the results. Tests only checks, so it should be irrelevant, shouldn't it?

Switching between capturing and noncapturing groups in test is relevant only if you use backreferences ( \\1 \\2 ). As you don't, the change has no effect.

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