在regex javascript中转义问号。

时间:2021-01-25 22:25:16

This is a simple question I think.

我认为这是一个简单的问题。

I am trying to search for the occurrence of a string in another string using regex in JavaScript like so:

我正在尝试使用JavaScript中的regex搜索另一个字符串中出现的字符串,如下所示:

 var content ="Hi, I like your Apartment. Could we schedule a viewing? My phone number is: ";

 var gent = new RegExp("I like your Apartment. Could we schedule a viewing? My", "g");

 if(content.search(gent) != -1){   
     alert('worked');     
 }          

This doesn't work because of the ? character....I tried escaping it with \, but that doesn't work either. Is there another way to use ? literally instead of as a special character?

因为?性格....我试着用\来逃避,但也不管用。还有其他使用方法吗?从字面上而不是作为一个特殊的角色?

4 个解决方案

#1


107  

You need to escape it with two backslashes

你需要用两个反斜杠来摆脱它

\\?

See this for more details:

详情如下:

http://www.trans4mind.com/personal_development/JavaScript/Regular%20Expressions%20Simple%20Usage.htm

http://www.trans4mind.com/personal_development/JavaScript/Regular%20Expressions%20Simple%20Usage.htm

#2


16  

You should use double slash:

你应该使用双斜杠:

var regex = new RegExp("\\?", "g");

Why? because in JavaScript the \ is also used to escape characters in strings, so: "\?" becomes: "?"

为什么?因为在JavaScript中,\也被用来转义字符串中的字符,所以:"\?"变成:"?"

And "\\?", becomes "\?"

和“\ \吗?”,成为“\ ?”

#3


15  

You can delimit your regexp with slashes instead of quotes and then a single backslash to escape the question mark. Try this:

您可以使用斜杠而不是引号来分隔regexp,然后使用一个反斜杠来避开问号。试试这个:

var gent = /I like your Apartment. Could we schedule a viewing\?/g;

#4


4  

Whenever you have a known pattern (i.e. you do not use a variable to build a RegExp), use literal regex notation where you only need to use single backslashes to escape special regex metacharacters:

当您有一个已知的模式(即您不使用一个变量来构建RegExp)时,请使用literal regex符号,您只需要使用单个的反斜杠来摆脱特殊的regex元字符:

var re = /I like your Apartment\. Could we schedule a viewing\?/g;
                               ^^                            ^^

Whenever you need to build a RegExp dynamically, use RegExp constructor notation where you MUST double backslashes for them to denote a literal backslash:

当您需要动态构建RegExp时,请使用RegExp构造函数表示法,其中您必须为它们设置双反斜杠,以表示文字的反斜杠:

var questionmark_block = "\\?"; // A literal ?
var initial_subpattern = "I like your Apartment\\. Could we schedule a viewing"; // Note the dot must also be escaped to match a literal dot
var re = new RegExp(initial_subpattern + questionmark_block, "g");

A must-read: RegExp: Description at MDN.

必须阅读:RegExp: MDN上的描述。

#1


107  

You need to escape it with two backslashes

你需要用两个反斜杠来摆脱它

\\?

See this for more details:

详情如下:

http://www.trans4mind.com/personal_development/JavaScript/Regular%20Expressions%20Simple%20Usage.htm

http://www.trans4mind.com/personal_development/JavaScript/Regular%20Expressions%20Simple%20Usage.htm

#2


16  

You should use double slash:

你应该使用双斜杠:

var regex = new RegExp("\\?", "g");

Why? because in JavaScript the \ is also used to escape characters in strings, so: "\?" becomes: "?"

为什么?因为在JavaScript中,\也被用来转义字符串中的字符,所以:"\?"变成:"?"

And "\\?", becomes "\?"

和“\ \吗?”,成为“\ ?”

#3


15  

You can delimit your regexp with slashes instead of quotes and then a single backslash to escape the question mark. Try this:

您可以使用斜杠而不是引号来分隔regexp,然后使用一个反斜杠来避开问号。试试这个:

var gent = /I like your Apartment. Could we schedule a viewing\?/g;

#4


4  

Whenever you have a known pattern (i.e. you do not use a variable to build a RegExp), use literal regex notation where you only need to use single backslashes to escape special regex metacharacters:

当您有一个已知的模式(即您不使用一个变量来构建RegExp)时,请使用literal regex符号,您只需要使用单个的反斜杠来摆脱特殊的regex元字符:

var re = /I like your Apartment\. Could we schedule a viewing\?/g;
                               ^^                            ^^

Whenever you need to build a RegExp dynamically, use RegExp constructor notation where you MUST double backslashes for them to denote a literal backslash:

当您需要动态构建RegExp时,请使用RegExp构造函数表示法,其中您必须为它们设置双反斜杠,以表示文字的反斜杠:

var questionmark_block = "\\?"; // A literal ?
var initial_subpattern = "I like your Apartment\\. Could we schedule a viewing"; // Note the dot must also be escaped to match a literal dot
var re = new RegExp(initial_subpattern + questionmark_block, "g");

A must-read: RegExp: Description at MDN.

必须阅读:RegExp: MDN上的描述。