Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript正则表达式:匹配电话号码_Javascript_Regex - Fatal编程技术网

javascript正则表达式:匹配电话号码

javascript正则表达式:匹配电话号码,javascript,regex,Javascript,Regex,如何在Javascript中匹配此电话号码 > str "(555) 555-3300" > str.match( '/\(555\) 555-3300/gi/' ) null > str.match( '/(555) 555-3300/gi/' ) null 我打赌有人能在2秒钟内回答这个问题。用谷歌搜索这个问题对我没有帮助 更新 在没有引用的情况下也进行了尝试: str.match(/\(555\) 555-3300/gi/) SyntaxError: Unexpecte

如何在Javascript中匹配此电话号码

> str
"(555) 555-3300"
> str.match( '/\(555\) 555-3300/gi/' )
null
> str.match( '/(555) 555-3300/gi/' )
null
我打赌有人能在2秒钟内回答这个问题。用谷歌搜索这个问题对我没有帮助

更新 在没有引用的情况下也进行了尝试:

str.match(/\(555\) 555-3300/gi/)
SyntaxError: Unexpected token )
str.match(/\(480\) 945-3300/g)

最终,我尝试用另一个电话号码替换该电话号码。

失去引号:

str.match(/\(555\) 555-3300/gi/)
SyntaxError: Unexpected token )
str.match(/\(480\) 945-3300/g)
失去报价:

str.match(/\(555\) 555-3300/gi/)
SyntaxError: Unexpected token )
str.match(/\(480\) 945-3300/g)

John的答案正确地定义了一个正则表达式来匹配该字符串——尽管我要指出,如果您想/精确/匹配该字符串,您最好使用indexOf():if(str.indexOf((480)945-3300))!=-1{/*字符串匹配*/}也尝试了:
str.match(/\(480\)945-3300/gi/)SyntaxError:Unexpected token)
stackoverflow没有格式化我的反斜杠,以便在最后一条评论中转义paren,但我确实把它们放进去了。有了标记,它将是
str.match(/\(480\)945-3300/gi)
——去掉后面的斜杠。不管怎样,我必须加倍转义paren:
new RegExp('\(480\)555-5555')
John的答案正确地定义了一个正则表达式来匹配该字符串-尽管我要指出,如果您想/准确地/匹配该字符串,您最好使用indexOf():if(str.indexOf((480)945-3300”)!=-1{/*字符串匹配*/}也尝试过:
str.match(/\(480\)945-3300/gi/)SyntaxError:Unexpected token)
stackoverflow没有格式化我的反斜杠来逃避上一条评论中的paren,但我确实把它们放进去了。有了标记,它将是
str.match(/\(480\)945-3300/gi)
——去掉后面的斜杠。不管怎样,我必须双重逃避paren:
new RegExp('\(480\\\\)555-5555')