根據一條線:

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

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

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

如何替代所有事件?


当前回答

如果链条包含类似的模式,如abccc,您可以使用以下模式:

str.replace(/abc(\s|$)/g, "")

其他回答

你可以尝试这样:

示例数据:

var text = "heloo,hai,hei"

text = text.replace(/[,]+/g, '')

text.forEach((value) => {
  hasil = hasil.replace(',', '')
})

2019年11月,新功能被添加到JavaScript, string.prototype.replaceAll()。

目前它仅支持巴比伦,但也许在未来它可以在所有浏览器中实施。

要替换一次,使用:

var res = str.replace('abc', "");

多次替换,使用:

var res = str.replace(/abc/g, "");
str = "Test abc test test abc test test test abc test test abc"

str.split(' ').join().replace(/abc/g,'').replace(/,/g, ' ')

如果链条包含类似的模式,如abccc,您可以使用以下模式:

str.replace(/abc(\s|$)/g, "")