I have grouping parentheses () and (?: ) and I need to match even if the expression within the brackets does not match. I’ve seen both | and ? used for this (i.e. (a|b|c|) and (a|b|c)?), but which should be used/is more efficient, and why?
As different JavaScript engines interpret regex differently, I’m specifically using the SpiderMonkey engine. A generalised (both language-wise and engine-wise) answer would be nice however.
Edit: A concrete example is the DuckDuckGo Frequency goodie. Why did the author choose | over ? in this case?
To check performance, see this fiddle.
Using
?with grouping or|with empty string as an option may lead to unexpected results!Couple tests:
This one ends with error:
And this one might be a solution for you:
Test the above code with this fiddle.