在正则表达式javascript中转义问号 [英] escaping question mark in regex javascript

查看:337
本文介绍了在正则表达式javascript中转义问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a simple question I think.

我试图在JavaScript中使用正则表达式搜索另一个字符串中字符串的出现,如下所示:

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

 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');     
 }          

由于,这不起作用? character ....我尝试用 \ 转义它,但这也不起作用。有没有其他方法可以使用字面而不是作为特殊字符?

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?

推荐答案

你需要用两个反斜杠来逃避它

You need to escape it with two backslashes

\\?

详情请见:

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

这篇关于在正则表达式javascript中转义问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆