Replace part of string that doesn't match regex

后端 未结 1 821
我寻月下人不归
我寻月下人不归 2020-12-11 23:00

I am attempting to replace parts of a string that don\'t match a regular expression pattern using JavaScript. This is functionally equivalent to using the <

相关标签:
1条回答
  • 2020-12-11 23:42

    If I understand you correctly, this should be one possible solution:

    'aforementioned'.replace(new RegExp(pattern + '|.', 'gi'), function(c) {
        return c === pattern ? c : '*';
    });
    
    >> "*fore*********"
    
    0 讨论(0)
提交回复
热议问题