Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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_Replace - Fatal编程技术网

Javascript 替换不符合';不匹配正则表达式

Javascript 替换不符合';不匹配正则表达式,javascript,regex,replace,Javascript,Regex,Replace,我试图用JavaScript替换字符串中与正则表达式模式不匹配的部分。这在功能上相当于在GNU grep中使用-v标志来反转结果。以下是一个例子: // I want to replace all characters that don't match "fore" // in "aforementioned" with "*" 'aforementioned'.replace(new RegExp(pattern, 'i'), function(match){ //generate

我试图用JavaScript替换字符串中与正则表达式模式不匹配的部分。这在功能上相当于在GNU grep中使用
-v
标志来反转结果。以下是一个例子:

// I want to replace all characters that don't match "fore" 
// in "aforementioned" with "*"

'aforementioned'.replace(new RegExp(pattern, 'i'), function(match){
    //generates a string of '*' that is the length of match
    return new Array(match.length).join('*');
});
我正在为
模式
寻找正则表达式。这可能与
(fore)
相反。我已经四处搜索过了,但是没有能够实现任何相关问题的答案来满足我的需要。下面是一个列表,也许它会为我们指明正确的方向:


    • 如果我理解正确,这应该是一种可能的解决方案:

      'aforementioned'.replace(new RegExp(pattern + '|.', 'gi'), function(c) {
          return c === pattern ? c : '*';
      });
      
      >> "*fore*********"
      

      这太棒了。谢谢:)