根據一條線:

s = "Test abc test test abc test test test abc test test abc";

这似乎只是在上面的行中删除ABC的第一次出现:

s = s.replace('abc', '');

如何替代所有事件?


当前回答

截至2020年8月,为ECMAScript提供了一个阶段4的提议,该提议将替代All 方法添加到 String。

它现在支持Chrome 85+,Edge 85+,Firefox 77+,Safari 13.1+。

使用方式与替代方法相同:

String.prototype.replaceAll(searchValue, replaceValue)

下面是使用例子:

'Test abc test test abc test.'.replaceAll('abc', 'foo'); // -> 'Test foo test test foo test.'

它在大多数现代浏览器中支持,但有多元化:

核心JS Es-Shims

它支持在V8发动机背后一个实验旗帜 - 和谐 - 带 - 替代。

其他回答

我知道这不是最好的办法,但你可以尝试一下:

var annoyingString = "Test abc test test abc test test test abc test test abc";

while (annoyingString.includes("abc")) {
    annoyingString = annoyingString.replace("abc", "")
}

我喜欢这个方法(看起来有点干净):

text = text.replace(new RegExp("cat","g"), "dog"); 

'a cat is not a caterpillar'.replace(/\bcat\b/gi,'dog');
//"a dog is not a caterpillar"

這是一個簡單的雷格斯,避免在大多數情況下取代字的部分. 然而,一個<unk> - 仍然被認為是字的邊界. 所以條件可以用在這種情況下,以避免取代線,如冷貓:

'a cat is not a cool-cat'.replace(/\bcat\b/gi,'dog');//wrong
//"a dog is not a cool-dog" -- nips
'a cat is not a cool-cat'.replace(/(?:\b([^-]))cat(?:\b([^-]))/gi,'$1dog$2');
//"a dog is not a cool-cat"

Regexp 不是唯一的替代多种现象的方法,远离它,思考灵活,思考分裂!

var newText = "the cat looks like a cat".split('cat').join('dog');

否则,要防止替代词部分 - 批准的答案也会做什么! 你可以通过常规的表达方式来围绕这个问题,我承认,有点复杂,并且作为一个惊喜,一个缓慢的,也:

var regText = "the cat looks like a cat".replace(/(?:(^|[^a-z]))(([^a-z]*)(?=cat)cat)(?![a-z])/gi,"$1dog");

结果与接受的答案相同,但是,在这个行上使用 /cat/g 表达式:

var oops = 'the cat looks like a cat, not a caterpillar or coolcat'.replace(/cat/g,'dog');
//returns "the dog looks like a dog, not a dogerpillar or cooldog" ??

var caterpillar = 'the cat looks like a cat, not a caterpillar or coolcat'.replace(/(?:(^|[^a-z]))(([^a-z]*)(?=cat)cat)(?![a-z])/gi,"$1dog");
//return "the dog looks like a dog, not a caterpillar or coolcat"

RegExp(常规表达式)对象 Regular-Expressions.info


在这种情况下,它显著简化表达,并提供更多的灵活性,如用正确的资本化替换或在一个行中替换两只猫和猫:

'Two cats are not 1 Cat! They\'re just cool-cats, you caterpillar'
   .replace(/(^|.\b)(cat)(s?\b.|$)/gi,function(all,char1,cat,char2)
    {
       // Check 1st, capitalize if required
       var replacement = (cat.charAt(0) === 'C' ? 'D' : 'd') + 'og';
       if (char1 === ' ' && char2 === 's')
       { // Replace plurals, too
           cat = replacement + 's';
       }
       else
       { // Do not replace if dashes are matched
           cat = char1 === '-' || char2 === '-' ? cat : replacement;
       }
       return char1 + cat + char2;//return replacement string
    });
//returns:
//Two dogs are not 1 Dog! They're just cool-cats, you caterpillar
function replaceAll(str, find, replace) {
    var $r="";
    while($r!=str){ 
        $r = str;
        str = str.replace(find, replace);
    }
    return str;
}
function replaceAll(str, find, replace) {
  var i = str.indexOf(find);
  if (i > -1){
    str = str.replace(find, replace); 
    i = i + replace.length;
    var st2 = str.substring(i);
    if(st2.indexOf(find) > -1){
      str = str.substring(0,i) + replaceAll(st2, find, replace);
    }       
  }
  return str;
}