[英]Why does JS method replace() work incorrect in the latest version of Safari?
Why does JS method replace()
work incorrect in the latest version of Safari (11.1) with "$<" in the second argument? 为什么JS方法replace()
在最新版本的Safari(11.1)中使用第二个参数中的“$ <”工作不正确?
Example: 例:
"aaaXXXbbb".replace(/XXX/, '_before_$<_after_')
Actual result: 实际结果:
"aaa$<_after_bbb"
Expected result: 预期结果:
"aaa_before_$<_after_bbb"
PS: "$$" inserts a "$" and solves this problem PS:“$$”插入“$”并解决了这个问题
This behavior doesn't occur in Firefox or Chrome, and I think I understand why. 在Firefox或Chrome中不会发生此问题,我想我理解为什么。
A new feature has been accepted for inclusion in the next version of the ECMAScript spec called 'Regex Named Capture Groups' which, as the name suggests, allows you to assign a name to a capture group in a regular expression. 一项新功能已被接受包含在ECMAScript规范的下一版本中,名为“Regex Named Capture Groups” ,顾名思义,它允许您在正则表达式中为捕获组指定名称。 One of the things added by this feature is a new addition to the replacement string syntax which allows you to interpolate the value of a named capture group. 此功能添加的功能之一是替换字符串语法的新增功能,允许您插入命名捕获组的值。
This syntax takes the form $<captureName>
- note the $<
at the beginning! 此语法采用$<captureName>
形式 - 请注意$<
开头!
According to the latest iteration of the spec , if you haven't closed a $<
with a corresponding >
, it should be ignored and just treated as part of the text. 根据规范的最新版本 ,如果你还没有用相应的>
关闭$<
,那么它应该被忽略并且只是作为文本的一部分处理。 The behavior being displayed in your browser doesn't line up with this, and therefore I think it's quite likely that you've discovered a bug! 浏览器中显示的行为与此不符,因此我认为您很可能发现了一个错误!
I'd encoruage you to report it to WebKit via the process detailed on their website . 我将通过他们网站上详细介绍的流程向您报告WebKit 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.