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 patible.
\((?>[^()]+|\((?<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?
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 patible.
\((?>[^()]+|\((?<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?
Share Improve this question edited May 4, 2011 at 16:16 entropo 2,48115 silver badges15 bronze badges asked May 4, 2011 at 16:03 Muhammad UsmanMuhammad Usman 12.5k6 gold badges38 silver badges60 bronze badges4 Answers
Reset to default 6JavaScript 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.co/114
Assuming that your given regular expression is a .NET regular expression, the following syntax ponents 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