- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
ECMAScript 2021 添加了一个新的字符串函数 replaceAll
.
很久以前,在一个不远的星系里,人们用split
+ join
或正则表达式到 replace all occurences of a string .
我创建了以下示例以将新方法与旧方法进行比较。
虽然我可以在第一种情况下看到一些差异,例如我不能使用 replacement patterns与 split
+ join
或者我需要使用 RegExp(str,"g")
转义特殊字符,我看不出第二种情况有什么不同。
新方法和旧方法有什么区别(行为差异、性能、浏览器兼容性……)?
const source = "abcdefabcdef";
const str1 = "abc", str2 = "xyz";
const reg1 = /abc/g, reg2 = "xyz";
//Case 1 : When we want to replace a string by another
console.log(source.split(str1).join(str2));
console.log(source.replace(new RegExp(str1,"g"),str2));
//versus
console.log(source.replaceAll(str1,str2));
//Case 2 : When we want to use a regular expression
console.log(source.replace(reg1,reg2));
//versus
console.log(source.replaceAll(reg1,reg2));
//Result = "xyzdefxyzdef"
最佳答案
从收集 documentation对于 replaceAll
,我们发现以下花絮:
const newStr = str.replaceAll(regexp|substr, newSubstr|function)
Note: When using a
regexp
you have to set the global ("g") flag; otherwise, it will throw a TypeError: "replaceAll must be called with a global RegExp".
replaceAll
使用正则表达式或
RegExp
,它必须使用全局标志。因此,调用
replaceAll
似乎并没有什么收获。与仅使用当前的
replace
相比.但是,与
replaceAll
有一个区别。就是当给它传递一个字符串时,它会自动进行全局替换。通过不必输入全局标志,您可以在此处节省一些打字时间。
关于JavaScript字符串替换与replaceAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67296652/
在 Java 正则表达式中使用尖括号时,“\\") 与 replaceAll (""),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/q
我有个小问题。我有一段文字,我必须在浏览器中阅读几次。每次,我打开这个文本,自动启动一个我写的 replaceAll。它非常简单、基本,但问题是当我下次替换时(每次我阅读这篇文章时)我有一个 repl
你好:D简短的问题:有什么区别 String geb = tf_datum.getText(); String sep = ""; //sep is short for seperator geb =
这个问题在这里已经有了答案: regex: How to escape backslashes and special characters? (1 个回答) 关闭 8 年前。 replaceAll
String.replaceAll() 和 Matcher.replaceAll()(在从 Regex.Pattern 创建的 Matcher 对象上)在性能方面是否存在已知差异? 另外,两者在高级
我需要从字符串中删除一些字符。目前我正在尝试删除“”(空格)和“-”(连字符)。我试过两种方法。他们都在运行 Android N Beta 的 Nexus 5X 上工作。它们都不适用于搭载 Andro
我在一些论坛上读到这样的神话,即通过执行类似的操作就足以通过 Veracode CWE 117(日志的不正确输出中和)问题。有人可以确认是否是这种情况吗? message.replaceAll("\
我想在不区分大小写的搜索中使用变量(而不是字符串)执行 JavaScript replaceAll(),同时保留匹配文本的大小写(在返回中)。例如, console.log('doc.p:', doc
这只是我尝试完成的事情的示例代码。 String s = "hello(1234aA)something"; String replaceString = "(1234aa)"; String s2
我在数据库的表中有一组原始文本,我需要使用一组单词替换此集合中的一些单词。 我将所有要替换的术语及其替代项放在一个文本文件中,如下所示 min=admin lelet=lambat lemot=lam
string 类中的方法 replaceAll(String regex, String replacement) 将此字符串中与给定正则表达式匹配的每个子字符串替换为给定的替换。是否可以否定正则表达
文档页面:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
我想转换这个字符串 M,L,XL,XXL 到 size M,size L,size XL,size XXL 使用replaceAll()方法。可能吗? 最佳答案 您可以使用积极的前瞻来做到这一点。 S
两个问题: 已解决 将 $ 替换为另一个特殊字符 @ 并解决了问题。问题1 我有一个变量 String strVar ,其中包含以下文本(它从文件中读取)。现在我尝试使用 replaceAll() 替
考虑以下字符串: String s = "/static/201105-3805-somerandom/images/optional-folder/filename.gif"; 如何删除“stati
我需要一个正则表达式来执行以下替换: "\"Id\":\"123Abc\",\"foo\":\"bar\"" -> "\"Id\":null,\"foo\":\"bar\"" (change valu
我最近完成了以下编程练习:Acronym Generator 声明如下: In nearly every company each employee has a certain acronym con
所以这段代码按预期工作: String test = "ONE: this is a\ntest string."; System.out.println(test); test = test.rep
我有一个大的 json 存储在字符串中。我想更改字符串的某些部分,但收到此错误: Exception in thread "main" java.util.regex.PatternSyntaxExc
我编写了以下代码行: str.replaceAll("xxx(ayyy)xxx", "$1".substring(0,1).equals("a") ? "a" : "b"); 但是我发现“$1”.su
我是一名优秀的程序员,十分优秀!