Cli*_*ote 4081 javascript string replace
我有这个字符串:
"Test abc test test abc test test test abc test test abc"
Run Code Online (Sandbox Code Playgroud)
干
str = str.replace('abc', '');
Run Code Online (Sandbox Code Playgroud)
似乎只删除abc上面字符串中的第一次出现.如何更换所有出现的内容?
Sea*_*ght 4137
str = str.replace(/abc/g, '');
Run Code Online (Sandbox Code Playgroud)
回应评论:
var find = 'abc';
var re = new RegExp(find, 'g');
str = str.replace(re, '');
Run Code Online (Sandbox Code Playgroud)
在回应Click Upvote的评论时,您可以进一步简化它:
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
Run Code Online (Sandbox Code Playgroud)
注意:正则表达式包含特殊(元)字符,因此盲目传递上述函数中的参数find而不预先处理它以逃避这些字符是危险的.这在Mozilla开发者网络的正则表达式JavaScript指南中有所介绍,它提供了以下实用程序功能:
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
Run Code Online (Sandbox Code Playgroud)
因此,为了使上述replaceAll()功能更安全,如果您还包括以下内容,可将其修改为以下内容escapeRegExp:
function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
Run Code Online (Sandbox Code Playgroud)
Cor*_*oss 2405
为了完整起见,我开始考虑使用哪种方法来完成这项工作.根据本页其他答案的建议,基本上有两种方法可以做到这一点.
注意:通常,建议不要在JavaScript中扩展内置原型.我仅仅为了说明的目的提供String原型的扩展,显示了String内置原型的假设标准方法的不同实现.
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
Run Code Online (Sandbox Code Playgroud)
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
Run Code Online (Sandbox Code Playgroud)
在效率方面我不太了解正则表达式如何在幕后工作,我倾向于倾向于分裂并在过去加入实现而不考虑性能.当我确实想知道哪个更有效率,以及在什么边缘时,我用它作为借口来找出答案.
在我的Chrome Windows 8计算机上,基于正则表达式的实现速度最快,分割和连接实现速度降低了53%.这意味着正则表达式的速度是我使用的lorem ipsum输入的两倍.
查看这个基准测试运行这两个实现.
正如@ThomasLeduc和其他人在下面的评论中所指出的,如果search包含在正则表达式中保留为特殊字符的某些字符,则基于正则表达式的实现可能存在问题.该实现假定调用者将事先转义字符串,或者只传递正则表达式(MDN)中没有表中字符的字符串.
MDN还提供了一种逃避字符串的实现.如果这也被标准化RegExp.escape(str),那将是很好的,但唉,它不存在:
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
Run Code Online (Sandbox Code Playgroud)
我们可以escapeRegExp在我们的String.prototype.replaceAll实现中调用,但是,我不确定这会对性能产生多大影响(甚至可能对于不需要转义的字符串,例如所有字母数字字符串).
Mat*_*ley 1300
注意:不要在实际代码中使用它.
作为简单文字字符串的正则表达式的替代,您可以使用
str = "Test abc test test abc test...".split("abc").join("");
Run Code Online (Sandbox Code Playgroud)
一般模式是
str.split(search).join(replacement)
Run Code Online (Sandbox Code Playgroud)
在某些情况下,这比使用replaceAll和正则表达式更快,但在现代浏览器中似乎不再是这种情况.因此,这应该只是用作快速入侵,以避免需要转义正则表达式,而不是真正的代码.
Ada*_*m A 631
使用带有g标志集的正则表达式将替换所有:
someString = 'the cat looks like a cat';
anotherString = someString.replace(/cat/g, 'dog');
// anotherString now contains "the dog looks like a dog"
Run Code Online (Sandbox Code Playgroud)
jes*_*sal 102
这是一个基于接受的答案的字符串原型函数:
String.prototype.replaceAll = function (find, replace) {
var str = this;
return str.replace(new RegExp(find, 'g'), replace);
};
Run Code Online (Sandbox Code Playgroud)
编辑
如果您find将包含特殊字符,那么您需要转义它们:
String.prototype.replaceAll = function (find, replace) {
var str = this;
return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};
Run Code Online (Sandbox Code Playgroud)
小提琴:http://jsfiddle.net/cdbzL/
Eli*_*gem 84
更新:
这是一个更新有点迟,但因为我偶然发现了这个问题,并注意到我之前的答案不是我很满意的.由于问题涉及替换单个单词,所以没有人想到使用单词边界(\b)
'a cat is not a caterpillar'.replace(/\bcat\b/gi,'dog');
//"a dog is not a caterpillar"
Run Code Online (Sandbox Code Playgroud)
这是一个简单的正则表达式,可以避免在大多数情况下替换部分单词.但是,破折号-仍被视为单词边界.因此在这种情况下可以使用条件来避免替换字符串,如cool-cat:
'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"
Run Code Online (Sandbox Code Playgroud)
基本上,这个问题与这里的问题相同: Javascript将"'"替换为"''"
@Mike,检查我给出的答案... regexp不是替换多次出现的替换的唯一方法,远非它.思考灵活,思考分裂!
var newText = "the cat looks like a cat".split('cat').join('dog');
Run Code Online (Sandbox Code Playgroud)
或者,为了防止替换单词部分 - 批准的答案也会这样做!您可以使用正则表达式来解决这个问题,我承认,这些正则表达式稍微复杂一些,并且作为结果,也有点慢:
var regText = "the cat looks like a cat".replace(/(?:(^|[^a-z]))(([^a-z]*)(?=cat)cat)(?![a-z])/gi,"$1dog");
Run Code Online (Sandbox Code Playgroud)
输出与接受的答案相同,但是,在此字符串上使用/ 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" ??
Run Code Online (Sandbox Code Playgroud)
哎呀,这可能不是你想要的.那是什么?恕我直言,一个只有条件地取代'猫'的正则表达式.(即不是单词的一部分),如下:
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"
Run Code Online (Sandbox Code Playgroud)
我的猜测是,这符合您的需求.当然,这不是全面的,但它应该足以让你开始.我建议在这些页面上阅读更多内容.这对于完善此表达式以满足您的特定需求非常有用.
http://www.javascriptkit.com/jsref/regexp.shtml
http://www.regular-expressions.info
最后补充:
鉴于这个问题仍然有很多观点,我想我可能会添加一个.replace与回调函数一起使用的例子.在这种情况下,它极大地简化表达,并提供了更大的灵活性,如大小写正确的更换或更换两个cat和cats一气呵成:
'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
Run Code Online (Sandbox Code Playgroud)
scr*_*ide 62
匹配全局正则表达式:
anotherString = someString.replace(/cat/g, 'dog');
Run Code Online (Sandbox Code Playgroud)
Adn*_*oky 52
These are the most common and readable methods.
var str = "Test abc test test abc test test test abc test test abc"
Run Code Online (Sandbox Code Playgroud)
Method-01:
str = str.replace(/abc/g, "replaced text");
Run Code Online (Sandbox Code Playgroud)
Method-02:
str = str.split("abc").join("replaced text");
Run Code Online (Sandbox Code Playgroud)
Method-03:
str = str.replace(new RegExp("abc", "g"), "replaced text");
Run Code Online (Sandbox Code Playgroud)
Method-04:
while(str.includes("abc")){
str = str.replace("abc", "replaced text");
}
Run Code Online (Sandbox Code Playgroud)
Output:
console.log(str);
// Test replaced text test test replaced text test test test replaced text test test replaced text
Run Code Online (Sandbox Code Playgroud)
Ind*_*ngh 52
要更换一次,请使用:
var res = str.replace('abc', "");
Run Code Online (Sandbox Code Playgroud)
要多次更换,请使用:
var res = str.replace(/abc/g, "");
Run Code Online (Sandbox Code Playgroud)
Sol*_*ogi 42
str = str.replace(/abc/g, '');
Run Code Online (Sandbox Code Playgroud)
或者从这里尝试replaceAll函数:
str = str.replaceAll('abc', ''); OR
var search = 'abc';
str = str.replaceAll(search, '');
Run Code Online (Sandbox Code Playgroud)
编辑:关于replaceAll可用性的澄清
'replaceAll'方法被添加到String的原型中.这意味着它可用于所有字符串对象/文字.
例如
var output = "test this".replaceAll('this', 'that'); //output is 'test that'.
output = output.replaceAll('that', 'this'); //output is 'test this'
Run Code Online (Sandbox Code Playgroud)
Emi*_*lía 39
假设您要将所有'abc'替换为'x':
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
Run Code Online (Sandbox Code Playgroud)
我试图考虑比修改字符串原型更简单的事情.
Ali*_*eza 35
使用RegExp中的JavaScript可以为你做这项工作,只是简单地做类似下面,不要忘了/g,之后杰出的事物为全球:
var str ="Test abc test test abc test test test abc test test abc";
str = str.replace(/abc/g, '');
Run Code Online (Sandbox Code Playgroud)
如果您考虑重用,请创建一个函数来为您完成,但不推荐它,因为它只是一个行函数,但如果你大量使用它,你可以写这样的东西:
String.prototype.replaceAll = String.prototype.replaceAll || function(string, replaced) {
return this.replace(new RegExp(string, 'g'), replaced);
};
Run Code Online (Sandbox Code Playgroud)
并简单地在代码中反复使用它,如下所示:
var str ="Test abc test test abc test test test abc test test abc";
str = str.replaceAll('abc', '');
Run Code Online (Sandbox Code Playgroud)
但是正如我前面提到的,它不会在要写入的行或性能方面产生巨大的差异,只有缓存函数可能会对长字符串产生更快的性能,如果你想重用它也是一个很好的DRY代码实践.
Kam*_*ski 32
今天 27.12.2019 我在macOS v10.13.6 (High Sierra) 上对所选的解决方案进行测试。
结论
str.replace(/abc/g, '');(c ^)是所有串良好的跨浏览器的快速的解决方案。split-join( A,B ) 或replace( C,D ) 的解决方案很快while( E,F,G,H ) 的解决方案很慢 - 通常对小字符串慢约 4 倍,对长字符串慢约 3000 倍 (!)我还创建了自己的解决方案。看起来目前它是完成问题工作的最短的一个:
str.split`abc`.join``
Run Code Online (Sandbox Code Playgroud)
str.split`abc`.join``
Run Code Online (Sandbox Code Playgroud)
测试在 Chrome 79.0、Safari 13.0.4 和 Firefox 71.0(64 位)上进行。测试RA和RB使用递归。结果
您可以在这里在您的机器上运行测试。Chrome 的结果:
递归解RA和RB给出
RangeError:超出最大调用堆栈大小
对于 100 万个字符,他们甚至破坏了 Chrome
我尝试为其他解决方案执行 1M 个字符的测试,但是E、F、G、H花费了太多时间,浏览器要求我中断脚本,因此我将测试字符串缩小到 275K 个字符。您可以在这里在您的机器上运行测试。铬的结果
测试中使用的代码
str = "Test abc test test abc test test test abc test test abc";
str = str.split`abc`.join``
console.log(str);Run Code Online (Sandbox Code Playgroud)
var t="Test abc test test abc test test test abc test test abc"; // .repeat(5000)
var log = (version,result) => console.log(`${version}: ${result}`);
function A(str) {
return str.split('abc').join('');
}
function B(str) {
return str.split`abc`.join``; // my proposition
}
function C(str) {
return str.replace(/abc/g, '');
}
function D(str) {
return str.replace(new RegExp("abc", "g"), '');
}
function E(str) {
while (str.indexOf('abc') !== -1) { str = str.replace('abc', ''); }
return str;
}
function F(str) {
while (str.indexOf('abc') !== -1) { str = str.replace(/abc/, ''); }
return str;
}
function G(str) {
while(str.includes("abc")) { str = str.replace('abc', ''); }
return str;
}
// src: https://stackoverflow.com/a/56989553/860099
function H(str)
{
let i = -1
let find = 'abc';
let newToken = '';
if (!str)
{
if ((str == null) && (find == null)) return newToken;
return str;
}
while ((
i = str.indexOf(
find, i >= 0 ? i + newToken.length : 0
)) !== -1
)
{
str = str.substring(0, i) +
newToken +
str.substring(i + find.length);
}
return str;
}
// src: https://stackoverflow.com/a/22870785/860099
function RA(string, prevstring) {
var omit = 'abc';
var place = '';
if (prevstring && string === prevstring)
return string;
prevstring = string.replace(omit, place);
return RA(prevstring, string)
}
// src: https://stackoverflow.com/a/26107132/860099
function RB(str) {
var find = 'abc';
var 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) + RB(st2, find, replace);
}
}
return str;
}
log('A ', A(t));
log('B ', B(t));
log('C ', C(t));
log('D ', D(t));
log('E ', E(t));
log('F ', F(t));
log('G ', G(t));
log('H ', H(t));
log('RA', RA(t)); // use reccurence
log('RB', RB(t)); // use reccurenceRun Code Online (Sandbox Code Playgroud)
Chr*_*ete 31
替换单引号:
function JavaScriptEncode(text){
text = text.replace(/'/g,''')
// More encode here if required
return text;
}
Run Code Online (Sandbox Code Playgroud)
Col*_*nce 25
这是不使用正则表达式的最快版本.
replaceAll = function(string, omit, place, prevstring) {
if (prevstring && string === prevstring)
return string;
prevstring = string.replace(omit, place);
return replaceAll(prevstring, omit, place, string)
}
Run Code Online (Sandbox Code Playgroud)
它几乎是分割和连接方法的两倍.
正如在这里的评论中指出的那样,如果您的omit变量包含place,则不会起作用replaceAll("string", "s", "ss"),因为它总是能够替换该单词的另一个出现.
在我的递归替换上还有另一个带有变体的jsperf甚至更快(http://jsperf.com/replace-all-vs-split-join/12)!
小智 24
//循环直到数字出现为0.或者只是复制/粘贴
function replaceAll(find, replace, str)
{
while( str.indexOf(find) > -1)
{
str = str.replace(find, replace);
}
return str;
}
Run Code Online (Sandbox Code Playgroud)
cso*_*akk 24
str = str.replace(new RegExp("abc", 'g'), "");
Run Code Online (Sandbox Code Playgroud)
对我来说比上面的答案更好.所以new RegExp("abc", 'g')创建一个RegExp匹配'g'文本("abc")的所有出现(标志).第二部分是替换为,在您的情况下空字符串("").
str是字符串,我们必须覆盖它,因为replace(...)只返回结果,但不是覆盖.在某些情况下,您可能想要使用它.
Ran*_*ner 23
String.prototype.replaceAll- ECMAScript 2021
新String.prototype.replaceAll()方法返回一个新字符串,其中模式的所有匹配项都被替换。模式可以是字符串或RegExp,替换可以是字符串或为每个匹配调用的函数。
const message = 'dog barks meow meow';
const messageFormatted = message.replaceAll('meow', 'woof')
console.log(messageFormatted);Run Code Online (Sandbox Code Playgroud)
rak*_*ice 21
如果您要查找的内容已经在字符串中,并且您没有方便的regex escaper,则可以使用join/split:
function replaceMulti(haystack, needle, replacement)
{
return haystack.split(needle).join(replacement);
}
someString = 'the cat looks like a cat';
console.log(replaceMulti(someString, 'cat', 'dog'));Run Code Online (Sandbox Code Playgroud)
Tim*_*oli 18
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;
}
Run Code Online (Sandbox Code Playgroud)
MMM*_*ION 18
当然,到了2021年,正确答案是:
\nconsole.log(\n \'Change this and this for me\'.replaceAll(\'this\',\'that\') // Normal case\n);\nconsole.log(\n \'aaaaaa\'.replaceAll(\'aa\',\'a\') // Challenged case\n);Run Code Online (Sandbox Code Playgroud)\r\n但如果浏览器是 2020 年之前的怎么办?
\n在这种情况下,我们需要polyfill(强制旧版浏览器支持新功能)(我认为几年内都是必要的)。\n我在答案中找不到完全正确的方法。所以我建议这个函数被定义为一个polyfill。
\nreplaceAll:replaceAllpolyfill(带有全局标志错误)(更原则的版本)if (!String.prototype.replaceAll) { // Check if the native function not exist\n Object.defineProperty(String.prototype, \'replaceAll\', { // Define replaceAll as a prototype for (Mother/Any) String\n configurable: true, writable: true, enumerable: false, // Editable & non-enumerable property (As it should be)\n value: function(search, replace) { // Set the function by closest input names (For good info in consoles)\n return this.replace( // Using native String.prototype.replace()\n Object.prototype.toString.call(search) === \'[object RegExp]\' // IsRegExp?\n ? search.global // Is the RegEx global?\n ? search // So pass it\n : function(){throw new TypeError(\'replaceAll called with a non-global RegExp argument\')}() // If not throw an error\n : RegExp(String(search).replace(/[.^$*+?()[{|\\\\]/g, "\\\\$&"), "g"), // Replace all reserved characters with \'\\\' then make a global \'g\' RegExp\n replace); // passing second argument\n }\n });\n}\nRun Code Online (Sandbox Code Playgroud)\nreplaceAllpolyfill(处理全局标志本身丢失)(我的第一选择)-为什么?if (!String.prototype.replaceAll) { // Check if the native function not exist\n Object.defineProperty(String.prototype, \'replaceAll\', { // Define replaceAll as a prototype for (Mother/Any) String\n configurable: true, writable: true, enumerable: false, // Editable & non-enumerable property (As it should be)\n value: function(search, replace) { // Set the function by closest input names (For good info in consoles)\n return this.replace( // Using native String.prototype.replace()\n Object.prototype.toString.call(search) === \'[object RegExp]\' // IsRegExp?\n ? search.global // Is the RegEx global?\n ? search // So pass it\n : RegExp(search.source, /\\/([a-z]*)$/.exec(search.toString())[1] + \'g\') // If not, make a global clone from the RegEx\n : RegExp(String(search).replace(/[.^$*+?()[{|\\\\]/g, "\\\\$&"), "g"), // Replace all reserved characters with \'\\\' then make a global \'g\' RegExp\n replace); // passing second argument\n }\n });\n}\nRun Code Online (Sandbox Code Playgroud)\nif(!String.prototype.replaceAll){Object.defineProperty(String.prototype,\'replaceAll\',{configurable:!0,writable:!0,enumerable:!1,value:function(search,replace){return this.replace(Object.prototype.toString.call(search)===\'[object RegExp]\'?search.global?search:RegExp(search.source,/\\/([a-z]*)$/.exec(search.toString())[1]+\'g\'):RegExp(String(search).replace(/[.^$*+?()[{|\\\\]/g,"\\\\$&"),"g"),replace)}})}\nRun Code Online (Sandbox Code Playgroud)\nif(!String.prototype.replaceAll){Object.defineProperty(String.prototype,\'replaceAll\',{configurable:!0,writable:!0,enumerable:!1,value:function(search,replace){return this.replace(Object.prototype.toString.call(search)===\'[object RegExp]\'?search.global?search:RegExp(search.source,/\\/([a-z]*)$/.exec(search.toString())[1]+\'g\'):RegExp(String(search).replace(/[.^$*+?()[{|\\\\]/g,"\\\\$&"),"g"),replace)}})}\n\nconsole.log(\n \'Change this and this for me\'.replaceAll(\'this\',\'that\')\n); // Change that and that for me\n\nconsole.log(\n \'aaaaaa\'.replaceAll(\'aa\',\'a\')\n); // aaa\n\nconsole.log(\n \'{} (*) (*) (RegEx) (*) (\\*) (\\\\*) [reserved characters]\'.replaceAll(\'(*)\',\'X\')\n); // {} X X (RegEx) X X (\\*) [reserved characters]\n\nconsole.log(\n \'How (replace) (XX) with $1?\'.replaceAll(/(xx)/gi,\'$$1\')\n); // How (replace) ($1) with $1?\n\nconsole.log(\n \'Here is some numbers 1234567890 1000000 123123.\'.replaceAll(/\\d+/g,\'***\')\n); // Here is some numbers *** *** *** and need to be replaced.\n\nconsole.log(\n \'Remove numbers under 233: 236 229 711 200 5\'.replaceAll(/\\d+/g, function(m) {\n return parseFloat(m) < 233 ? \'\' : m\n })\n); // Remove numbers under 233: 236 711\n\nconsole.log(\n \'null\'.replaceAll(null,\'x\')\n); // x\n\n\n// The difference between My first preference and the original:\n// Now in 2022 with browsers > 2020 it should throw an error (But possible it be changed in future)\n\n// console.log(\n// \'xyz ABC abc ABC abc xyz\'.replaceAll(/abc/i,\'\')\n// );\n\n// Browsers < 2020:\n// xyz xyz\n// Browsers > 2020\n// TypeError: String.prototype.replaceAll called with a non-global RegExpRun Code Online (Sandbox Code Playgroud)\r\n如果第一个参数输入为:\n , , , , , ... , , , , ... ,结果与本机replaceAll 相同nullundefinedObjectFunctionDateRegExpNumberString
参考:22.1.3.19 String.prototype.replaceAll ( searchValue, ReplaceValue) \n + RegExp 语法
\n重要提示:正如一些专业人士提到的那样,答案中建议的许多递归函数将返回错误的结果。(尝试使用上面代码片段的有挑战性的情况。)\n也许一些棘手的方法(例如或一些.split(\'searchValue\').join(\'replaceValue\')管理良好的函数)给出相同的结果,但性能肯定比native replaceAll()//低得多polyfill replaceAll()replace() + RegExp
例如,我们也可以支持 IE7+,只需不使用Object.defineProperty()并使用我旧的幼稚赋值方法:
\nif (!String.prototype.replaceAll) {\n String.prototype.replaceAll = function(search, replace) { // <-- Naive method for assignment\n // ... (Polyfill code Here)\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n它应该可以很好地满足 IE7+ 的基本用途。
\n但正如@sebastian-simon所解释的,这可能会在更高级的用途中产生次要问题。例如:
for (var k in \'hi\') console.log(k);\n// 0\n// 1\n// replaceAll <-- ?\nRun Code Online (Sandbox Code Playgroud)\n事实上,我建议的选择有点乐观。就像我们信任的环境(浏览器和Node.js)一样,它肯定是在 2012-2021 年左右。而且它是标准/著名的,因此不需要任何特殊考虑。
\n但甚至可能存在较旧的浏览器或一些意想不到的问题,polyfills 仍然可以支持和解决更多可能的环境问题。因此,如果我们需要尽可能多的支持,我们可以使用polyfill 库,例如:
\n\n特别适用于replaceAll:
\n<script src="https://polyfill.io/v3/polyfill.min.js?features=String.prototype.replaceAll"></script>\nRun Code Online (Sandbox Code Playgroud)\n
Owe*_*wen 16
我喜欢这种方法(它看起来更干净):
text = text.replace(new RegExp("cat","g"), "dog");
Run Code Online (Sandbox Code Playgroud)
pkd*_*dkk 13
var str = "ff ff f f a de def";
str = str.replace(/f/g,'');
alert(str);
Run Code Online (Sandbox Code Playgroud)
小智 13
while (str.indexOf('abc') !== -1)
{
str = str.replace('abc', '');
}
Run Code Online (Sandbox Code Playgroud)
小智 13
如果字符串包含类似的模式abccc,您可以使用:
str.replace(/abc(\s|$)/g, "")
Run Code Online (Sandbox Code Playgroud)
Oli*_*ech 13
经过多次尝试和多次失败后,我发现以下功能在浏览器兼容性和易用性方面似乎是最好的全能工具。这是我发现的唯一适用于旧版浏览器的解决方案。(是的,尽管旧的浏览器不被鼓励并且已经过时,但一些遗留应用程序仍然大量使用OLE浏览器(例如旧的Visual Basic 6应用程序或带有表单的Excel .xlsm 宏。)
无论如何,这是一个简单的函数。
function replaceAll(str, match, replacement){
return str.split(match).join(replacement);
}
Run Code Online (Sandbox Code Playgroud)
Bla*_*ack 12
以前的答案太复杂了.只需使用这样的替换功能:
str.replace(/your_regex_pattern/g, replacement_string);
Run Code Online (Sandbox Code Playgroud)
例:
var str = "Test abc test test abc test test test abc test test abc";
var res = str.replace(/[abc]+/g, "");
console.log(res);Run Code Online (Sandbox Code Playgroud)
saj*_*dre 12
在不使用任何正则表达式的情况下,最简单的方法是在此处拆分并加入类似代码的代码:
var str="Test abc test test abc test test test abc test test abc";
str.split('abc').join('')
Run Code Online (Sandbox Code Playgroud)
Tho*_*ita 12
截至 2020 年 8 月,ECMAScript的第 4 阶段提案将该replaceAll方法添加到String.
现在支持Chrome 85+、Edge 85+、Firefox 77+、Safari 13.1+。
用法同replace方法:
String.prototype.replaceAll(searchValue, replaceValue)
Run Code Online (Sandbox Code Playgroud)
这是一个示例用法:
'Test abc test test abc test.'.replaceAll('abc', 'foo'); // -> 'Test foo test test foo test.'
Run Code Online (Sandbox Code Playgroud)
大多数现代浏览器都支持它,但存在 polyfill:
它在V8引擎中受实验标志支持--harmony-string-replaceall。在V8 网站上阅读更多信息。
Sam*_*ody 10
如果您尝试确保即使在替换后您要查找的字符串也不存在,则需要使用循环.
例如:
var str = 'test aabcbc';
str = str.replace(/abc/g, '');
Run Code Online (Sandbox Code Playgroud)
完成后,你仍然会'测试abc'!
解决这个问题的最简单的循环是:
var str = 'test aabcbc';
while (str != str.replace(/abc/g, '')){
str.replace(/abc/g, '');
}
Run Code Online (Sandbox Code Playgroud)
但是每次循环都会进行两次替换.也许(有被投票的风险)可以合并为一个稍微更有效但可读性更低的形式:
var str = 'test aabcbc';
while (str != (str = str.replace(/abc/g, ''))){}
// alert(str); alerts 'test '!
Run Code Online (Sandbox Code Playgroud)
在查找重复的字符串时,这可能特别有用.
例如,如果我们有'a ,,, b',我们希望删除所有重复的逗号.
[在这种情况下,可以做.replace(/,+/g,','),但在某些时候,正则表达式变得复杂而且足够缓慢而不是循环.
Che*_*ode 10
尽管人们已经提到了正则表达式的使用,但是如果你想要替换文本而不管文本的情况如何,那么有更好的方法.像大写或小写.使用以下语法
//Consider below example
originalString.replace(/stringToBeReplaced/gi, '');
//Output will be all the occurrences removed irrespective of casing.
Run Code Online (Sandbox Code Playgroud)
您可以在此处参考详细示例.
fra*_*cis 10
使用正则表达式i标记不区分大小写
console.log('App started'.replace(/a/g, '')) // Result: "App strted"
console.log('App started'.replace(/a/gi, '')) // Result: "pp strted"
Run Code Online (Sandbox Code Playgroud)
您可以简单地使用以下方法
/**
* Replace all the occerencess of $find by $replace in $originalString
* @param {originalString} input - Raw string.
* @param {find} input - Target key word or regex that need to be replaced.
* @param {replace} input - Replacement key word
* @return {String} Output string
*/
function replaceAll(originalString, find, replace) {
return originalString.replace(new RegExp(find, 'g'), replace);
};
Run Code Online (Sandbox Code Playgroud)
小智 7
只需添加 /g
document.body.innerHTML = document.body.innerHTML.replace('hello', 'hi');
Run Code Online (Sandbox Code Playgroud)
至
// Replace 'hello' string with /hello/g regular expression.
document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi');
Run Code Online (Sandbox Code Playgroud)
/g 意味着全球化
我用p来存储前一个递归替换的结果:
function replaceAll(s, m, r, p) {
return s === p || r.contains(m) ? s : replaceAll(s.replace(m, r), m, r, s);
}
Run Code Online (Sandbox Code Playgroud)
它将替换字符串s中的所有匹配项,直到可能为止:
replaceAll('abbbbb', 'ab', 'a') ? 'abbbb' ? 'abbb' ? 'abb' ? 'ab' ? 'a'
Run Code Online (Sandbox Code Playgroud)
为了避免无限循环,我检查替换r是否包含匹配m:
replaceAll('abbbbb', 'a', 'ab') ? 'abbbbb'
Run Code Online (Sandbox Code Playgroud)
小智 7
以下功能适用于我:
String.prototype.replaceAllOccurence = function(str1, str2, ignore)
{
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
} ;
Run Code Online (Sandbox Code Playgroud)
现在调用这样的函数:
"you could be a Project Manager someday, if you work like this.".replaceAllOccurence ("you", "I");
Run Code Online (Sandbox Code Playgroud)
只需将此代码复制并粘贴到浏览器控制台中即可.
检查这个答案可能会有所帮助,我在我的项目中使用过。
function replaceAll(searchString, replaceString, str) {
return str.split(searchString).join(replaceString);
}
replaceAll('abc', '',"Test abc test test abc test test test abc test test abc" ); // "Test test test test test test test test "
Run Code Online (Sandbox Code Playgroud)
大多数人可能会这样做来编码URL.要对URL进行编码,您不仅应考虑空格,还应正确转换整个字符串encodeURI.
encodeURI("http://www.google.com/a file with spaces.html")
Run Code Online (Sandbox Code Playgroud)
要得到:
http://www.google.com/a%20file%20with%20spaces.html
Run Code Online (Sandbox Code Playgroud)
方法一
尝试实现一个正则表达式:
"test abc test test abc test test test abc test test abc".replace(/\abc/g, ' ');
方法二
拆分并加入。用 abc 分割并用空格连接。
"test abc test test abc test test test abc test test abc".split("abc").join(" ")
这可以使用正则表达式来实现。一些可能对某人有所帮助的组合:
var word = "this,\\ .is*a*test, '.and? / only / 'a \ test?";
var stri = "This is a test and only a test";
Run Code Online (Sandbox Code Playgroud)
要替换所有非字母字符,
console.log(word.replace(/([^a-z])/g,' ').replace(/ +/g, ' '));
Result: [this is a test and only a test]
Run Code Online (Sandbox Code Playgroud)
用一个空格替换多个连续空格,
console.log(stri.replace(/ +/g,' '));
Result: [This is a test and only a test]
Run Code Online (Sandbox Code Playgroud)
要替换所有 * 字符,
console.log(word.replace(/\*/g,''));
Result: [this,\ .isatest, '.and? / only / 'a test?]
Run Code Online (Sandbox Code Playgroud)
替换问号 (?)
console.log(word.replace(/\?/g,'#'));
Result: [this,\ .is*a*test, '.and# / only / 'a test#]
Run Code Online (Sandbox Code Playgroud)
要替换引号,
console.log(word.replace(/'/g,'#'));
Result: [this,\ .is*a*test, #.and? / only / #a test?]
Run Code Online (Sandbox Code Playgroud)
要替换所有 ' 字符,
console.log(word.replace(/,/g,''));
Result: [this\ .is*a*test '.and? / only / 'a test?]
Run Code Online (Sandbox Code Playgroud)
要替换特定单词,
console.log(word.replace(/test/g,''));
Result: [this,\ .is*a*, '.and? / only / 'a ?]
Run Code Online (Sandbox Code Playgroud)
要替换反斜杠,
console.log(word.replace(/\\/g,''));
Result: [this, .is*a*test, '.and? / only / 'a test?]
Run Code Online (Sandbox Code Playgroud)
要替换正斜杠,
console.log(word.replace(/\//g,''));
Result: [this,\ .is*a*test, '.and? only 'a test?]
Run Code Online (Sandbox Code Playgroud)
要替换所有空格,
console.log(word.replace(/ /g,'#'));
Result: [this,\#.is*a*test,####'.and?#/#only#/#####'a##test?]
Run Code Online (Sandbox Code Playgroud)
要替换点,
console.log(word.replace(/\./g,'#'));
Result: [this,\ #is*a*test, '#and? / only / 'a test?]
Run Code Online (Sandbox Code Playgroud)
我的实现,很自我解释
function replaceAll(string, token, newtoken) {
if(token!=newtoken)
while(string.indexOf(token) > -1) {
string = string.replace(token, newtoken);
}
return string;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
要替换所有字符,请尝试以下代码:
Suppose we have need to send " and \ in my string, then we will convert it " to \" and \ to \\
Run Code Online (Sandbox Code Playgroud)
因此,该方法将解决此问题。
String.prototype.replaceAll = function (find, replace) {
var str = this;
return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};
var message = $('#message').val();
message = message.replaceAll('\\', '\\\\'); /*it will replace \ to \\ */
message = message.replaceAll('"', '\\"'); /*it will replace " to \\"*/
Run Code Online (Sandbox Code Playgroud)
我正在使用Ajax,并且需要以JSON格式发送参数。然后我的方法如下所示:
function sendMessage(source, messageID, toProfileID, userProfileID) {
if (validateTextBox()) {
var message = $('#message').val();
message = message.replaceAll('\\', '\\\\');
message = message.replaceAll('"', '\\"');
$.ajax({
type: "POST",
async: "false",
contentType: "application/json; charset=utf-8",
url: "services/WebService1.asmx/SendMessage",
data: '{"source":"' + source + '","messageID":"' + messageID + '","toProfileID":"' + toProfileID + '","userProfileID":"' + userProfileID + '","message":"' + message + '"}',
dataType: "json",
success: function (data) {
loadMessageAfterSend(toProfileID, userProfileID);
$("#<%=PanelMessageDelete.ClientID%>").hide();
$("#message").val("");
$("#delMessageContainer").show();
$("#msgPanel").show();
},
error: function (result) {
alert("message sending failed");
}
});
}
else {
alert("Please type message in message box.");
$("#message").focus();
}
}
String.prototype.replaceAll = function (find, replace) {
var str = this;
return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我使用了为此目的最强大的自定义函数,即使将split/join解决方案包装在更简单的情况下,它在Chrome 60和Firefox 54 ( JSBEN.CH) 中也比其他解决方案快一点。我的电脑运行Windows 7 64 bits。
优点是这个自定义函数可以使用字符串或字符同时处理许多替换,这对于某些应用程序来说可以是一种快捷方式。
像split/join上面的解决方案一样,下面的解决方案没有转义字符的问题,与正则表达式方法不同。
function replaceAll(s,find,repl,caseOff,byChar){
if (arguments.length<2) return false;
var destDel = ! repl; // if destDel delete all keys from target
var isString = !! byChar; // if byChar, replace set of characters
if (typeof find !==typeof repl && ! destDel) return false;
if (isString && (typeof find!=="string")) return false;
if (! isString && (typeof find==="string")) {
return s.split(find).join(destDel?"":repl);
}
if ((! isString) && ( ! Array.isArray(find) ||
( ! Array.isArray(repl) && ! destDel) )) return false;
// if destOne replace all strings/characters by just one element
var destOne = destDel ? false : (repl.length===1);
// Generally source and destination should have the same size
if (! destOne && ! destDel && find.length!==repl.length) return false
var prox,sUp,findUp,i,done;
if (caseOff) { // case insensitive
// Working with uppercase keys and target
sUp = s.toUpperCase();
if (isString)
findUp = find.toUpperCase()
else
findUp = find.map(function(el){ return el.toUpperCase();});
} else { // case sensitive
sUp = s;
findUp =find.slice(); // clone array/string
}
done = new Array(find.length); // size: number of keys
done.fill(null);
var pos = 0; // initial position in target s
var r = ""; // initial result
var aux, winner;
while (pos < s.length) { // Scanning the target
prox = Number.MAX_SAFE_INTEGER;
winner = -1; // no winner at start
for (i=0;i<findUp.length;i++) // find next occurence for each string
if (done[i]!==-1) { // key still alive
// Never search for the word/char or is over?
if (done[i]===null || done[i]<pos) {
aux = sUp.indexOf(findUp[i],pos);
done[i]=aux; // Save the next occurrence
} else
aux = done[i] // restore the position of last search
if (aux<prox && aux!==-1) { // if next occurrence is minimum
winner = i; // save it
prox = aux;
}
} // not done
if (winner===-1) { // No matches forward
r += s.slice(pos);
break;
} // no winner
// found the character or string key in the target
i = winner; // restore the winner
r += s.slice(pos,prox); // update piece before the match
// Append the replacement in target
if (! destDel) r += repl[ destOne?0:i ];
pos = prox + ( isString?1:findUp[i].length ); // go after match
} // loop
return r; // return the resulting string
}
Run Code Online (Sandbox Code Playgroud)
文档如下
Run Code Online (Sandbox Code Playgroud)replaceAll Syntax ====== replaceAll(s,find,[ repl ,caseOff, byChar) Parameters ========== "s" is a string target of replacement. "find" can be a string or array of strings. "repl" should be the same type than "find" or empty if "find" is a string, it is a simple replacement for all "find" occurrences in "s" by string "repl" if "find" is an array, it will replaced each string in "find" that occurs in "s" for corresponding string in "repl" array. The replace specs are independent: A replacement part cannot be replaced again. if "repl" is empty all "find" occurrences in "s" will be deleted. if "repl" has only one character or element, all occurrences in "s" will be replaced for that one. "caseOff" is true if replacement is case insensitive (default is FALSE) "byChar" is true when replacement is based on set of characters. Default is false if "byChar", it will be replaced in "s" all characters in "find" set of characters for corresponding character in "repl" set of characters Return ====== the function returns the new string after the replacement.
公平地说,我在没有参数测试的情况下运行了基准测试。
这是我的测试集,使用 Node.js
function l() { return console.log.apply(null, arguments); }
var k=0;
l(++k,replaceAll("banana is a ripe fruit harvested near the river",
["ri","nea"],["do","fa"])); //1
l(++k,replaceAll("banana is a ripe fruit harvested near the river",
["ri","nea"],["do"])); //2
l(++k,replaceAll("banana is a ripe fruit harvested near the river",
["ri","nea"])); //3
l(++k,replaceAll("banana is a ripe fruit harvested near the river",
"aeiou","","",true)); //4
l(++k,replaceAll("banana is a ripe fruit harvested near the river",
"aeiou","a","",true)); //5
l(++k,replaceAll("banana is a ripe fruit harvested near the river",
"aeiou","uoiea","",true)); //6
l(++k,replaceAll("banana is a ripe fruit harvested near the river",
"aeiou","uoi","",true)); //7
l(++k,replaceAll("banana is a ripe fruit harvested near the river",
["ri","nea"],["do","fa","leg"])); //8
l(++k,replaceAll("BANANA IS A RIPE FRUIT HARVESTED NEAR THE RIVER",
["ri","nea"],["do","fa"])); //9
l(++k,replaceAll("BANANA IS A RIPE FRUIT HARVESTED NEAR THE RIVER",
["ri","nea"],["do","fa"],true)); //10
return;
Run Code Online (Sandbox Code Playgroud)
结果:
1 '香蕉是一种在多佛远处收获的水果'
2 '香蕉是一种在多 佛多佛
收获的水果' 3 '香蕉是在 ver 中收获的 pe 水果'
4 'bnn s rp frt hrvstd nr th rvr'
5 '香蕉as a rapa fraat harvastad naar tha ravar'
6 'bununu is u ripo frait hurvostod nour tho rivor'
7 false
8 false
9 'BANANA 是一种
在河边收获的成熟水果' 10 'BANANA IS ARUESTED DOPERED'
在与主要答案相关的性能方面,这些是一些在线测试。
尽管以下是一些使用的性能测试console.time()(它们在您自己的控制台中效果最佳,但是这段时间很短,无法在代码段中看到)
console.time('split and join');
"javascript-test-find-and-replace-all".split('-').join(' ');
console.timeEnd('split and join')
console.time('regular expression');
"javascript-test-find-and-replace-all".replace(new RegExp('-', 'g'), ' ');
console.timeEnd('regular expression');
console.time('while');
let str1 = "javascript-test-find-and-replace-all";
while (str1.indexOf('-') !== -1) {
str1 = str1.replace('-', ' ');
}
console.timeEnd('while');Run Code Online (Sandbox Code Playgroud)
有趣的是,如果多次运行它们,即使RegExp解决方案平均来看最快,而while循环解决方案最慢,结果却总是不同的。
我使用split and join或此功能
function replaceAll( text, busca, reemplaza ){
while (text.toString().indexOf(busca) != -1)
text = text.toString().replace(busca,reemplaza);
return text;
}
Run Code Online (Sandbox Code Playgroud)
现在已经有一个完整的提案可以集成String.prototype.replaceAll到官方规范中。最终,开发人员将不必提出自己的实现replaceAll- 相反,现代 JavaScript 引擎将原生支持它。
该提案处于第四阶段,这意味着一切都已完成,剩下的就是浏览器开始实施它。
它已在最新版本的 Chrome、Firefox 和 Safari 中发布。
以下是实施细节:
根据当前的 TC39 共识,在所有情况下
String.prototype.replaceAll的行为都相同,String.prototype.replace,但以下两种情况除外:
- 如果
searchValue是字符串,String.prototype.replace则仅替换单个出现的searchValue,而String.prototype.replaceAll替换所有出现的searchValue( 就好像.split(searchValue).join(replaceValue)使用了全局且正确转义的正则表达式一样)。- 如果
searchValue是非全局正则表达式,String.prototype.replace则替换单个匹配项,而String.prototype.replaceAll引发异常。这样做是为了避免缺少全局标志(这意味着“不替换全部”)和被调用方法的名称(强烈建议“替换全部”)之间固有的混淆。值得注意的是,
String.prototype.replaceAll行为就像String.prototype.replaceifsearchValue是全局正则表达式一样。
在支持的环境中,以下代码片段将记录foo-bar-baz,而不会引发错误:
const str = 'foo bar baz';
console.log(
str.replaceAll(' ', '-')
);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3190678 次 |
| 最近记录: |