根據一條線:
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', '');
如何替代所有事件?
当前回答
我使用分割和加入或这个功能:
function replaceAll(text, busca, reemplaza) {
while (text.toString().indexOf(busca) != -1)
text = text.toString().replace(busca, reemplaza);
return text;
}
其他回答
以前的答案太复杂了,只需使用替代功能如下:
str.replace(/your_regex_pattern/g, replacement_string);
例子:
var str = “测试 abc 测试 abc 测试 abc 测试 abc”; var res = str.replace(/[abc]+/g, ""); console.log(res);
如果链条包含类似的模式,如abccc,您可以使用以下模式:
str.replace(/abc(\s|$)/g, "")
你可以尝试这样:
示例数据:
var text = "heloo,hai,hei"
text = text.replace(/[,]+/g, '')
或
text.forEach((value) => {
hasil = hasil.replace(',', '')
})
方法1
尝试执行一个常见的表达式:
“测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试
方法2
与ABC分开并加入空空间。
“测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试ABC测试
var str = "ff ff f f a de def";
str = str.replace(/f/g,'');
alert(str);
HTTP://jsfiddle.net/ANHR9/