简体   繁体   English

替换字符串中的所有匹配项,并避免RegExp转义

[英]Replace all occurrences in a string and avoid RegExp escaping

Is that possible, without a loss of performance, to replace all occurrences of a substring with another string, and completely avoid using RegExp along the way? 是否可以在不损失性能的情况下,用另一个字符串替换所有出现的子字符串,并完全避免在使用过程中使用RegExp? Ie remove RegExp out of equation, just to make sure there is no RegExp magic happening when you forget to properly escape something like + . 即从方程式中删除RegExp,只是为了确保当您忘记正确地转义+类的字符时不会发生RegExp魔术。

'1+1 2+2'.replace('+', '-') -> only first "+" is replaced
'1+1 2+2'.replace(/\+/g, '-') -> undesired regexp complexities

UPDATE 1 更新1

This does not solve the escape problem: 这不能解决转义问题:

String.prototype.replaceAll= function(search, replace) {
    return this.replace(new RegExp(search, "g"), replace);
}

UPDATE 2 更新2

Doing programmatic unescape is a performance hit: 进行程序化unescape会降低性能:

RegExp.quote = function(str) {
    return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
};

Alternatively, based on Florian Margaine's code and test case: 或者,根据Florian Margaine的代码和测试用例:

    window.replaceQuick = function(subject, search, replace) {
      var index = -1, offset = 0;
      var result = '';
      while ((index = subject.indexOf(search, offset)) !== -1) {
        result += subject.substring(offset, index) + replace;
        offset = index + search.length;
      }
      return result + search.substring(offset);
    }

Since I'm doing partial extractions it should get a better performance. 由于我正在进行部分提取,因此应该可以获得更好的性能。

http://jsperf.com/replaceall-regex-or-not/3 http://jsperf.com/replaceall-regex-or-not/3

function replaceAll(str, search, replace) {
    while (str.indexOf(search) > -1) {
        str = str.replace(search, replace);
    }
    return str;
}

This works. 这可行。 However, is it more performant that using a regex? 但是,使用正则表达式是否更具性能? Let's try it. 让我们尝试一下。

Here is the regex function I benchmarked against: 这是我基准测试的正则表达式函数:

function replaceAllRegex(str, search, replace) {
    return str.replace(new RegExp(search.replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&"), 'g'), replace);
}

According to jsperf , the non-regex versions does ~8k ops/sec, while the regex version does ~123k ops/sec. 根据jsperf的说法 ,非正则表达式版本的运行速度为〜8k ops / sec,而正则表达式版本的运行速度为〜123k ops / sec。 When automatically escaping the characters. 自动转义字符时。

You should review your view of "escaping is a performance hit". 您应该查看“逃避是性能上的损失”的观点。

If what you want is performance, then use the regex version. 如果要获得性能,请使用正则表达式版本。

PS: Roel's version can be faster than regex. PS: Roel的版本可能比regex快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM