简体   繁体   English

在所有现代浏览器中,javascript正则表达式中的花括号处理是否相同?

[英]Is handling of curly braces in javascript regex is the same across all modern browsers?

Curly braces in JavaScript regex is used to denote quantifiers. JavaScript正则表达式中的大括号用于表示量词。 So writing 所以写作

a{2,4}

will match aa, aaa and aaaa. 将匹配aa,aaa和aaaa。 But if you mistype this quantifier like this: 但是如果你错误地输入这个量词,就像这样:

x{1,x}

It will match the literal text "x{1,x}", at least in Firefox. 它将匹配文字文本“x {1,x}”,至少在Firefox中。

Is this behavior common to modern browsers? 这种行为在现代浏览器中是否常见?

The ECMA standard prohibits this behavior and requires the escaping of the braces. ECMA标准禁止这种行为,并要求撤销支架。

(Background: I have to write a parser for javascript regexes at work.) (背景:我必须在工作中为javascript正则表达式编写一个解析器。)

I don't know for JavaScript and browsers, but this is the behaviour I would have expected and that I have seen in the past in regular expressions. 我不知道JavaScript和浏览器,但这是我期望的行为,我在过去的正则表达式中看到过。

So I tested different regex engines on their behaviour: 所以我测试了不同的正则表达式引擎的行为:

  • C# : behaves this way C# :表现得这样

  • Perl : behaves this way Perl :表现得这样

  • Python : behaves this way Python :表现得这样

  • PHP : behaves this way PHP :表现得这样

  • Java : throws an Exception Java :抛出异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM