如何在JavaScript中执行str_replace,替换JavaScript中的文本? [英] How can I perform a str_replace in JavaScript, replacing text in JavaScript?

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

问题描述

我想使用 str_replace 或类似替代方法来替换JavaScript中的一些文字。

I want to use str_replace or its similar alternative to replace some text in JavaScript.

var text = "this is some sample text that i want to replace";
var new_text = replace_in_javascript("want", "dont want", text);
document.write("new_text");

应该给予

this is some sample text that i dont want to replace




如果你要使用正则表达式,
与内置替换方法相比有什么性能影响。

If you are going to regex, what are the performance implications in comparison to the built in replacement methods.


推荐答案

使用正则表达式替换字符串比使用字符串替换要慢得多。

如演示在JSPerf上,你可以有不同的效率来创建一个正则表达式,但是所有这些都比简单的字符串替换慢得多。 正则表达式较慢因为

Using regex for string replacement is significantly slower than using a string replace.
As demonstrated on JSPerf, you can have different levels of efficiency for creating a regex, but all of them are significantly slower than a simple string replace. The regex is slower because:


固定字符串匹配没有回溯,编译步骤,范围,字符类或许多其他减慢正则表达式引擎的功能。有一些方法可以优化正则表达式匹配,但我认为在常见情况下不可能将索引编入字符串。

Fixed-string matches don't have backtracking, compilation steps, ranges, character classes, or a host of other features that slow down the regular expression engine. There are certainly ways to optimize regex matches, but I think it's unlikely to beat indexing into a string in the common case.

在JS perf页面上运行简单测试,我已经记录了一些结果:

For a simple test run on the JS perf page, I've documented some of the results:

<script>
// Setup
  var startString = "xxxxxxxxxabcxxxxxxabcxx";
  var endStringRegEx = undefined;
  var endStringString = undefined;
  var endStringRegExNewStr = undefined;
  var endStringRegExNew = undefined;
  var endStringStoredRegEx = undefined;      
  var re = new RegExp("abc", "g");
</script>

<script>
// Tests
  endStringRegEx = startString.replace(/abc/g, "def") // Regex
  endStringString = startString.replace("abc", "def", "g") // String
  endStringRegExNewStr = startString.replace(new RegExp("abc", "g"), "def"); // New Regex String
  endStringRegExNew = startString.replace(new RegExp(/abc/g), "def"); // New Regexp
  endStringStoredRegEx = startString.replace(re, "def") // saved regex
</script>

Chrome 68的结果如下:

The results for Chrome 68 are as follows:

String replace:    9,936,093 operations/sec
Saved regex:       5,725,506 operations/sec
Regex:             5,529,504 operations/sec
New Regex String:  3,571,180 operations/sec
New Regex:         3,224,919 operations/sec

从这个答案的完整性(借鉴评论)开始,值得一提的是。 replace 仅替换匹配字符的第一个实例。它只能用 // g 替换所有实例。如果替换多个实例 name.replace('','_'),则性能权衡和代码优雅可能会更糟。替换('','_')。replace('', '_'); 或更糟 while(name.includes('')){name = name.replace('','_')}

From the sake of completeness of this answer (borrowing from the comments), it's worth mentioning that .replace only replaces the first instance of the matched character. Its only possible to replace all instances with //g. The performance trade off and code elegance could be argued to be worse if replacing multiple instances name.replace(' ', '_').replace(' ', '_').replace(' ', '_'); or worse while (name.includes(' ')) { name = name.replace(' ', '_') }

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

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