Javascript搜索函数read '*'和& # 39;强生# 39;是一样的

javascript search function read '*' and '.' as the same

本文关键字:一样 强生 函数 Javascript 搜索 read      更新时间:2023-09-26

我有麻烦与我的javascript代码,因为匹配函数读取'*'和'。

代码如下:

var value = "sample*com";
var rules = new Array(".com ",".net",".org",".us",".ca",".com");
var found = 0;
for (var i = 0; i < rules.length; i++) {
   var filter = new RegExp('''b' + rules[i] + '''b', 'gi');
   if(value.match(filter)) {
      found++;
      console.log("found .com");
   } 
}

问题是,为什么脚本检测*为'.'?因为控制台说"found .com"即使值= "sample*com"

请帮忙,提前感谢!

需要将.com中的.转义为'.com。否则点匹配任何字符。因此,在您的规则数组中,您必须将其转义为''.com