JavaScript RegEx Syntax Error - Stack Overflow

I have created the following Regular Expression in C# to extract tokens from a string in the format {to

I have created the following Regular Expression in C# to extract tokens from a string in the format {token}. I developed the regex in C# and confirmed that it works.

// c#
var regex = new Regex(
    "\\{ (?>[^{}]+| \\{ (?<number>) | \\} (?<-number>) )*(?(number)(?!))\\}",
    RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | 
    RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

var matches = regex.Matches("blah {token1} blah blah  blah {token2} blah");

The variable matches ends up containing two matches - "{token1}" and "{token2}".

I need to execute this in JavaScript, but I get a Syntax error in the expression when I try to execute the following line...

// JavaScript
var regex = new RegExp("\\{ (?>[^{}]+| \\{ (?<number>) | \\} (?<-number>) )*(?(number)(?!))\\}");

Is there anything obvious that I am doing wrong?
Am I trying to use RegEx features that are not supported in JavaScript?

I have created the following Regular Expression in C# to extract tokens from a string in the format {token}. I developed the regex in C# and confirmed that it works.

// c#
var regex = new Regex(
    "\\{ (?>[^{}]+| \\{ (?<number>) | \\} (?<-number>) )*(?(number)(?!))\\}",
    RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | 
    RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

var matches = regex.Matches("blah {token1} blah blah  blah {token2} blah");

The variable matches ends up containing two matches - "{token1}" and "{token2}".

I need to execute this in JavaScript, but I get a Syntax error in the expression when I try to execute the following line...

// JavaScript
var regex = new RegExp("\\{ (?>[^{}]+| \\{ (?<number>) | \\} (?<-number>) )*(?(number)(?!))\\}");

Is there anything obvious that I am doing wrong?
Am I trying to use RegEx features that are not supported in JavaScript?

Share Improve this question asked Dec 15, 2010 at 10:13 Andy McCluggageAndy McCluggage 38.8k18 gold badges61 silver badges71 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If you want to match all occurences of "{something}" try:

\{[^\}]*\}

Or is there another condition that has to be met? like {token[0-9]}?

http://rubular./ can help with testing.

Independent subexpressions qualified with (?> ... ) aren't supported in Javascript regular expressions. The ? is actually evaluated as a quantifier.

The main point is that if you use C# (or Java) you use a string to write the regular expression. In such a string you need to escape all escaping characters again, which is why you need "\\{" to escape a "{". JavaScript however supports its own syntax of regular expression similar to Perl and PHP I think.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744774098a4592939.html

相关推荐

  • JavaScript RegEx Syntax Error - Stack Overflow

    I have created the following Regular Expression in C# to extract tokens from a string in the format {to

    15小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信