Bra*_*non 3 javascript regex replace
我试图使用JavaScript 替换与正则表达式模式不匹配的字符串部分.这在功能上等同于使用-v
GNU grep中的标志来反转结果.这是一个例子:
// 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('*');
});
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个正则表达式pattern
.这将是相反的(fore)
.我已经四处搜索但未能实现任何相关问题的答案以满足我的需求.无论如何,这是一个列表,也许它会指出我们正确的方向:
如果我理解正确,这应该是一个可能的解决方案:
'aforementioned'.replace(new RegExp(pattern + '|.', 'gi'), function(c) {
return c === pattern ? c : '*';
});
>> "*fore*********"
Run Code Online (Sandbox Code Playgroud)