Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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/2/google-app-engine/4.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,我有一个字符串str=“C-IND-Mom&;s>s Restaurantzxc”。我想删除特殊字符&使用regex从中获取,但测试失败&在替换特殊字符后,我得到了相同的字符串 这就是我所做的: str=str.replace('/\&/g', '&'); 有人能告诉我哪里出了问题吗?您需要使用 str=str.replace(/&;/g,,&')不带引号/ie regex match,否则将被视为字符串匹配可能与预期输出重复?注意,您在正则表达式周围

我有一个字符串
str=“C-IND-Mom&;s>s Restaurantzxc”
。我想删除特殊字符
&使用regex从中获取,但测试失败&在替换特殊字符后,我得到了相同的字符串

这就是我所做的:

str=str.replace('/\&/g', '&');
有人能告诉我哪里出了问题吗?

您需要使用


str=str.replace(/&;/g,,&')
不带引号/ie regex match,否则将被视为字符串匹配

可能与预期输出重复?注意,您在正则表达式周围有
'
,请删除它们:
str=str.replace(/&;/g'&')。看,很好。保留在
g
修饰符中。@trincot谢谢你忘了那个。此外,我不认为字符串匹配会做全局匹配,所以删除了该代码