根據一條線:
s = "Test abc test test abc test test test abc test test abc";
这似乎只是在上面的行中删除ABC的第一次出现:
s = s.replace('abc', '');
如何替代所有事件?
根據一條線:
s = "Test abc test test abc test test test abc test test abc";
这似乎只是在上面的行中删除ABC的第一次出现:
s = s.replace('abc', '');
如何替代所有事件?
当前回答
在与主要答案相关的性能方面,这些是某些在线测试。
虽然以下是使用 console.time() 的某些性能测试(它们在自己的控制台上工作最好,因为时间很短,可以在下面的剪辑中看到)。
值得注意的是,如果你运行它们多次,结果总是不同的,尽管正常的表达解决方案似乎是最快的平均,而旋转解决方案是最慢的。
其他回答
我知道这不是最好的办法,但你可以尝试一下:
var annoyingString = "Test abc test test abc test test test abc test test abc";
while (annoyingString.includes("abc")) {
annoyingString = annoyingString.replace("abc", "")
}
以前的答案太复杂了,只需使用替代功能如下:
str.replace(/your_regex_pattern/g, replacement_string);
例子:
var str = “测试 abc 测试 abc 测试 abc 测试 abc”; var res = str.replace(/[abc]+/g, ""); console.log(res);
对抗全球常规表达:
anotherString = someString.replace(/cat/g, 'dog');
替换单一引用:
function JavaScriptEncode(text){
text = text.replace(/'/g,''')
// More encode here if required
return text;
}
str = "Test abc test test abc test test test abc test test abc"
str.split(' ').join().replace(/abc/g,'').replace(/,/g, ' ')