Small enhancement to allow non-capturing parentheses in regexes while still warning about capturing parens

This simple patch to src/components/common.js allows the use of non-capturing parens in regexes. e.g. (?:www\.)example\.com
--- src/components/common.js 2010-10-03 15:22:48.000000000 +1100
+++ src/components/common.js 2010-10-03 15:55:58.000000000 +1100
@@ -115,9 +115,18 @@
else if (p.indexOf("*") == -1 && p.indexOf("?") == -1 &&
!fp.warnings.showWarningIfDesired(win, ["no.wildcard.characters", p], "wildcards"))
return false;
- // Check for parenthesis without backslash
- if (new RegExp("[^\\\\]\\(|[^\\\\]\\)", "g").test(p) &&
- !fp.warnings.showWarningIfDesired(win, ["no.parentheses"], "parentheses")) {
+ // Check for capturing or unbalanced parentheses without backslash
+ var noEscapes = p.replace(/\\./, '', 'g');
+ if (isRegEx) {
+ // Repeatedly remove innermost non-capturing parentheses until none are left
+ var noEscapesCopy;
+ do {
+ noEscapesCopy = noEscapes;
+ noEscapes = noEscapes.replace(/\(\?[:=!][^()]*\)/, '', 'g');
+ } while (noEscapes != noEscapesCopy);
+ }
+ if (new RegExp('[()]').test(noEscapes)) &&
+ !fp.warnings.showWarningIfDesired(win, ["no.parentheses"], "parentheses")) {
return false;
}
return p;

Hm, thanks, but are you sure

Hm, thanks, but are you sure this won't break anything else?

Yes I am

I've been using regexes with non-capturing parentheses for quite a while and they work perfectly except for having to either turn the warning off or continually clicking through it on those rules with them.

There is a small error in the diff though. I accidentally left an extra close parenthesis on that last if statement just before the &&

It should be : if (new RegExp('[()]').test(noEscapes) &&

The manipulation of the pattern is actually done on a copy so the patch doesn't affect what is returned.
Reviewing the surrounding code suggests I should move the 'g' flag for replace onto the end of the literal regexes like is done earlier in the validatePattern function to trim surrounding whitespace.