If you don't want the match to be remembered why not just abandon parentheses altogether? What's the advantage of using /(?:x)/ instead of just /x/?
It used when you want to group chars, but you don't want to remember them. If you remove the parentheses, you can't group a sequence of characters. So, for instance:
/foo|bar/.test("foo") // true
/foo|bar/.test("bar") // true
If you want to achieve a pattern that match foor
and fbar
you have to group the characters in the middle:
/f(?:oo|ba)r/.test("fbar") // true
/f(?:oo|ba)r/.test("foor") // true
Of course you can also group and remember the group, but if instead test
your using match
or other methods where the array returned is significant to you, then you have to use (?:
instead.