开发者

How can this regex be made JavaScript compatible?

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 ")"');
}


There's no negative lookbehind in Javascript. You'll have to find an alternative.

Edit: I looked at your RegEx again, and I thought I saw negative lookbehind, but I guess I didn't. I'll leave this for informational purposes anyways.


In javascript, numbers can be defined with [0-9] or \d Not a digit is [^0-9] or \D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜