简体   繁体   English

无法转义“?”,但“ \\”在javascript正则表达式中工作正常

[英]Not able to escape “?” but “\” works fine in javascript regex

I have following example (in node): 我有以下示例(在节点中):

var reg = new RegExp("aa\?b", 'g');
var msgg = "aa?b"

if(msgg.match(reg)){
    console.log(1);
} else {
    console.log(0);
}

This prints 0 or returns null. 打印0或返回null。 I don't understand why it works if ? 我不明白为什么它会起作用? is replaced with \\ but not in case of ?. 用\\代替,但用?代替。 Is ? 是吗 some more special than any others?? 比其他任何东西更特别?

You need to double escape, like this: 您需要两次转义,如下所示:

var reg = new RegExp("aa\\?b", 'g');

Or use RegExp literal: 或使用RegExp文字:

 var reg = /aa\?b/g;

Reason is that JavaScript string "\\?" 原因是JavaScript字符串"\\?" evaluates to "?" 评估为"?" because ? 因为? is not a special escape character. 不是特殊的转义字符。 Hence your RegExp receives a literal question mark and not an escaped one. 因此,您的RegExp会收到文字问号,而不是转义的问号。 Double escaping ensures the "\\" is treated literally. 两次转义可确保对“ \\”进行逐字处理。

Passing the literal notation instead of the string notation to the constructor your regexp works: 文字符号而不是字符串符号传递给构造函数,您的regexp可以工作:

 var reg = new RegExp(/aa\\?b/, 'g'); var msgg = "aa?b" console.log(msgg.match(reg)) 

From MDN : MDN

There are 2 ways to create a RegExp object: a literal notation and a constructor. 有两种创建RegExp对象的方法:文字符号和构造函数。 To indicate strings, the parameters to the literal notation do not use quotation marks while the parameters to the constructor function do use quotation marks. 为了指示字符串,文字符号的参数不使用引号,而构造函数的参数则使用引号。 So the following expressions create the same regular expression: 因此,以下表达式创建相同的正则表达式:

/ab+c/i;
new RegExp('ab+c', 'i');
new RegExp(/ab+c/, 'i');

The literal notation provides a compilation of the regular expression when the expression is evaluated. 文字表示法在评估表达式时提供了正则表达式的编译。 Use literal notation when the regular expression will remain constant. 当正则表达式保持不变时,请使用文字符号。 For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration. 例如,如果使用文字符号构造循环中使用的正则表达式,则不会在每次迭代时重新编译正则表达式。

The constructor of the regular expression object, for example, new RegExp('ab+c') , provides runtime compilation of the regular expression. 正则表达式对象的构造函数,例如new RegExp('ab+c') ,提供了正则表达式的运行时编译。 Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input. 当您知道正则表达式模式将要更改时,或者您不知道该模式并从其他来源(例如用户输入)获取该模式时,请使用构造函数。

Starting with ECMAScript 6, new RegExp(/ab+c/, 'i') no longer throws a TypeError ("can't supply flags when constructing one RegExp from another") when the first argument is a RegExp and the second flags argument is present. 从ECMAScript 6开始,当第一个参数是RegExp而第二个标志参数时, new RegExp(/ab+c/, 'i')不再引发TypeError(“从另一个构造RegExp时不能提供标志”)存在。 A new RegExp from the arguments is created instead. 而是根据参数创建一个新的RegExp。

When using the constructor function, the normal string escape rules (preceding special characters with \\ when included in a string) are necessary. 使用构造函数时,需要正常的字符串转义规则(当包含在字符串中时,在特殊字符前加\\)。 For example, the following are equivalent: 例如,以下等同:

var re = /\w+/;
var re = new RegExp('\\w+');

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

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