如何在JavaScript中替换所有出现的字符串 [英] How to replace all occurrences of a string in JavaScript

查看:104
本文介绍了如何在JavaScript中替换所有出现的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串:

"Test abc test test abc test test test abc test test abc"

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

似乎只删除第一次出现的 abc 在上面的字符串中。如何替换所有次出现?

seems to only remove the first occurrence of abc in the string above. How can I replace all occurrences of it?

推荐答案

为了完整起见,我得到了想着我应该用哪种方法来做这件事。根据此页面上的其他答案的建议,基本上有两种方法可以执行此操作。

For the sake of completeness, I got to thinking about which method I should use to do this. There are basically two ways to do this as suggested by the other answers on this page.

注意:一般情况下,扩展内置通常不建议使用JavaScript中的原型。我仅仅为了说明的目的提供String原型的扩展,在 String 内置原型上显示假设标准方法的不同实现。

Note: In general, extending the built-in prototypes in JavaScript is generally not recommended. I am providing as extensions on the String prototype simply for purposes of illustration, showing different implementations of a hypothetical standard method on the String built-in prototype.

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};



拆分和加入(功能)实施



Split and Join (Functional) Implementation

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.split(search).join(replacement);
};






不太了解正则表达式如何工作在效率方面,我倾向于倾向于分裂并在过去加入实施而不考虑性能。当我确实想知道哪个更有效率,以及什么保证金时,我用它作为借口来查找。


Not knowing too much about how regular expressions work behind the scenes in terms of efficiency, I tended to lean toward the split and join implementation in the past without thinking about performance. When I did wonder which was more efficient, and by what margin, I used it as an excuse to find out.

在我的Chrome Windows  8机器上,基于正则表达式的实现是最快的拆分和连接实现慢了53%。这意味着正则表达式的速度是我使用的lorem ipsum输入的两倍。

On my Chrome Windows 8 machine, the regular expression based implementation is the fastest, with the split and join implementation being 53% slower. Meaning the regular expressions are twice as fast for the lorem ipsum input I used.

看看这个 基准

正如@ThomasLeduc和其他人在下面的评论中所指出的,如果 search 包含某些保留为<的字符,则基于正则表达式的实现可能会出现问题a href =https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_special_characters =noreferrer>正则表达式中的特殊字符。该实现假定调用者将事先转义字符串,或者只传递 正则表达式 (MDN)。

As noted in the comment below by @ThomasLeduc and others, there could be an issue with the regular expression-based implementation if search contains certain characters which are reserved as special characters in regular expressions. The implementation assumes that the caller will escape the string beforehand or will only pass strings that are without the characters in the table in Regular Expressions (MDN).

MDN还提供了一个实现逃避我们的弦。如果它也被标准化为 RegExp.escape(str),那将会很好,但唉,它不存在:

MDN also provides an implementation to escape our strings. It would be nice if this was also standardized as RegExp.escape(str), but alas, it does not exist:

function escapeRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

我们可以调用 escapeRegExp 在我们的 String.prototype.replaceAll 实现中,但是,我不确定这会对性能产生多大影响(甚至可能对于不需要转义,就像所有字母数字字符串一样。)

We could call escapeRegExp within our String.prototype.replaceAll implementation, however, I'm not sure how much this will affect the performance (potentially even for strings for which the escape is not needed, like all alphanumeric strings).

这篇关于如何在JavaScript中替换所有出现的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆