Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何替换JavaScript中出现的所有字符串_Javascript_String_Replace - Fatal编程技术网

如何替换JavaScript中出现的所有字符串

如何替换JavaScript中出现的所有字符串,javascript,string,replace,Javascript,String,Replace,我的JavaScript代码中有以下字符串: "Test abc test test abc test test test abc test test abc" 做: str = str.replace('abc', ''); 似乎只删除上面字符串中第一个出现的abc 如何替换它的所有出现项?使用带有g标志集的正则表达式将替换所有: someString = 'the cat looks like a cat'; anotherString = someString.re

我的JavaScript代码中有以下字符串:

"Test abc test test abc test test test abc test test abc"
做:

str = str.replace('abc', '');
似乎只删除上面字符串中第一个出现的
abc


如何替换它的所有出现项?

使用带有
g
标志集的正则表达式将替换所有:

someString = 'the cat looks like a cat';
anotherString = someString.replace(/cat/g, 'dog');
// anotherString now contains "the dog looks like a dog"

使用带有
g
标志集的正则表达式将替换所有:

someString = 'the cat looks like a cat';
anotherString = someString.replace(/cat/g, 'dog');
// anotherString now contains "the dog looks like a dog"

匹配全局正则表达式:

anotherString = someString.replace(/cat/g, 'dog');
str.replace(/abc/g, '');

匹配全局正则表达式:

anotherString = someString.replace(/cat/g, 'dog');
str.replace(/abc/g, '');

自2020年8月起:用于ECMAScript 2021语言规范定义的


对于旧版/旧版浏览器:

str = str.replace(/abc/g, "replaced text");
str = str.split("abc").join("replaced text");
str = str.replace(new RegExp("abc", "g"), "replaced text");
while(str.includes("abc")){
    str = str.replace("abc", "replaced text");
}
str=str.replace(/abc/g');
针对评论:

var find='abc';
var re=新的RegExp(查找“g”);
str=str.replace(re.);
对于的评论,您可以进一步简化:

函数replaceAll(str、find、replace){
返回str.replace(新的RegExp(查找'g'),replace);
}
注意:正则表达式包含特殊(元)字符,因此,在上面的
find
函数中盲目传递参数而不进行预处理以转义这些字符是危险的。在的中介绍了这一点,其中他们提供了以下实用程序功能(自最初编写此答案以来,该功能已更改至少两次,因此请确保检查MDN站点以了解可能的更新):

函数escapeRegExp(字符串){
返回字符串。replace(/[.*+\-?^${}()\[\]\\]/g,\\$&');/$&表示整个匹配字符串
}
因此,为了使上面的
replaceAll()
函数更安全,如果还包括
escapeRegExp
,则可以将其修改为以下内容:

函数replaceAll(str、find、replace){
return str.replace(新的RegExp(escapeRegExp(find),'g'),replace);
}

自2020年8月起:用于ECMAScript 2021语言规范定义的


对于旧版/旧版浏览器:

str = str.replace(/abc/g, "replaced text");
str = str.split("abc").join("replaced text");
str = str.replace(new RegExp("abc", "g"), "replaced text");
while(str.includes("abc")){
    str = str.replace("abc", "replaced text");
}
str=str.replace(/abc/g');
针对评论:

var find='abc';
var re=新的RegExp(查找“g”);
str=str.replace(re.);
对于的评论,您可以进一步简化:

函数replaceAll(str、find、replace){
返回str.replace(新的RegExp(查找'g'),replace);
}
注意:正则表达式包含特殊(元)字符,因此,在上面的
find
函数中盲目传递参数而不进行预处理以转义这些字符是危险的。在的中介绍了这一点,其中他们提供了以下实用程序功能(自最初编写此答案以来,该功能已更改至少两次,因此请确保检查MDN站点以了解可能的更新):

函数escapeRegExp(字符串){
返回字符串。replace(/[.*+\-?^${}()\[\]\\]/g,\\$&');/$&表示整个匹配字符串
}
因此,为了使上面的
replaceAll()
函数更安全,如果还包括
escapeRegExp
,则可以将其修改为以下内容:

函数replaceAll(str、find、replace){
return str.replace(新的RegExp(escapeRegExp(find),'g'),replace);
}
str=str.replace(/abc/g');
或者尝试以下建议的
replaceAll
方法:

str=str.replaceAll('abc','');
或:

var搜索='abc';
str=str.replaceAll(搜索“”);
编辑:关于
replaceAll
可用性的澄清

replaceAll
方法被添加到
String
的原型中。这意味着它将可用于所有字符串对象/文字

例如:

var output=“testthis”.replaceAll('this'、'that');//输出为“testthat”。
output=output.replaceAll('that','this');//输出为“测试此”
str=str.replace(/abc/g');
或者尝试以下建议的
replaceAll
方法:

str=str.replaceAll('abc','');
或:

var搜索='abc';
str=str.replaceAll(搜索“”);
编辑:关于
replaceAll
可用性的澄清

replaceAll
方法被添加到
String
的原型中。这意味着它将可用于所有字符串对象/文字

例如:

var output=“testthis”.replaceAll('this'、'that');//输出为“testthat”。
output=output.replaceAll('that','this');//输出为“测试此”

使用正则表达式:

anotherString = someString.replace(/cat/g, 'dog');
str.replace(/abc/g, '');

使用正则表达式:

anotherString = someString.replace(/cat/g, 'dog');
str.replace(/abc/g, '');

更新:在最流行浏览器的最新版本中,您可以使用 如图所示:

let result=“1 abc 2 abc 3.replaceAll”(“abc”,“xyz”);
//`result`是“1xyz2xyz3”
但是,首先检查或其他兼容性表,以确保您针对的浏览器已经添加了对它的支持


有关节点和与旧浏览器/非当前浏览器的兼容性:

注意:不要在性能关键代码中使用以下解决方案。

作为简单文字字符串正则表达式的替代方法,您可以使用

str = "Test abc test test abc test...".split("abc").join("");
一般的模式是

str.split(search).join(replacement)
在某些情况下,这比使用
replaceAll
和正则表达式更快,但在现代浏览器中似乎不再如此

基准:


结论:如果您有一个性能关键的用例(例如处理数百个字符串),请使用Regexp方法。但是对于大多数典型的用例,不必担心特殊字符是很值得的。

更新:在最流行的浏览器的最新版本中,您可以使用 如图所示:

let result=“1 abc 2 abc 3.replaceAll”(“abc”,“xyz”);
//`result`是“1xyz2xyz3”
但是检查或其他兼容性表
while (str.indexOf('abc') !== -1)
{
    str = str.replace('abc', '');
}
var str = 'test aabcbc';
str = str.replace(/abc/g, '');
var str = 'test aabcbc';
while (str != str.replace(/abc/g, '')){
   str.replace(/abc/g, '');
}
var str = 'test aabcbc';
while (str != (str = str.replace(/abc/g, ''))){}
// alert(str); alerts 'test '!
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;
}
document.body.innerHTML = document.body.innerHTML.replace('hello', 'hi');
// Replace 'hello' string with /hello/g regular expression.
document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi');
function JavaScriptEncode(text){
    text = text.replace(/'/g,''')
    // More encode here if required

    return text;
}
//Consider below example
originalString.replace(/stringToBeReplaced/gi, '');

//Output will be all the occurrences removed irrespective of casing.
let some_str = 'abc def def lom abc abc def'.split('abc').join('x')
console.log(some_str) //x def def lom x x def
var str ="Test abc test test abc test test test abc test test abc";
str = str.replace(/abc/g, '');
String.prototype.replaceAll = String.prototype.replaceAll || function(string, replaced) {
  return this.replace(new RegExp(string, 'g'), replaced);
};
var str ="Test abc test test abc test test test abc test test abc";
str = str.replaceAll('abc', '');
str.replace(/abc(\s|$)/g, "")
str = str.replace(new RegExp("abc", 'g'), "");
str.replace(/your_regex_pattern/g, replacement_string);
var str = "Test abc test test abc test test test abc test test abc"
str = str.replace(/abc/g, "replaced text");
str = str.split("abc").join("replaced text");
str = str.replace(new RegExp("abc", "g"), "replaced text");
while(str.includes("abc")){
    str = str.replace("abc", "replaced text");
}
console.log(str);
// Test replaced text test test replaced text test test test replaced text test test replaced text
var res = str.replace('abc', "");
var res = str.replace(/abc/g, "");
String.prototype.replaceAll(searchValue, replaceValue)
'Test abc test test abc test.'.replaceAll('abc', 'foo'); // -> 'Test foo test test foo test.'
str.split`abc`.join``