I have found a regex template in Expresso, this one works fine and returns perfect matches but in JavaScript it doesn't work. I know its may be for look-behind, but I am not enough efficient in Regex to make it JS compatible.
\((?>[^()]+|\((?<number>)|\)(?<-number>))*(?(number)(?!))\)
I want to match it with ...
max(50, max(51, 60)) a() MAX(s,4,455)something
... and it should return ...
1: (50, max(51, 60))
2: ()
3: (s,4,455)
This works perfectly in Expresso but JS console in Chrome says:
Uncaught SyntaxError: Invalid regular expression: /\((?>[^()]+|\((?<number>)|\)(?<-number>))*(?(number)(?!))\)/: Invalid group
How can this regex be modified to function properly in JavaScript?
JavaScript does not support named capture groups (?<number>)
As suggested in this question, regex may not be the right tool for the job. However, if you simply want to match the outermost parenthesized groups you can use this
/\([^)]*(\s?\))*\)/g
See it working at http://refiddle.com/114
Assuming that your given regular expression is a .NET regular expression, the following syntax components are not supported by JavaScript:
(?>…) – nonbacktracking subexpression (?<name>…) – named matched subexpressions
(?<name1-name2>…) – balancing group definition
(?(name)…) – conditional group
To get the same result with JavaScript, you’ll need to do the balancing on your own:
var parts = str.match(/[()]|[^()]+/g),
    matches = [],
    balance = 0;
for (var i=0, j=0; i<parts.length; i++) {
    switch (parts[i]) {
    case "(":
        if (balance === 0) {
            j = i;
        }
        balance++;
        break;
    case ")":
        if (balance === 1) {
            matches.push(parts.slice(j, i+1).join(""));
        }
        balance--;
        if (balance < 0) {
            throw new EvalError('parentheses are not balanced; unexpected "("');
        }
        break;
    }
}
if (balance > 0) {
    throw new EvalError('parentheses are not balanced; missing ")"');
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With