Why does JS method replace()
work incorrect in the latest version of Safari (11.1) with "$<" in the second argument?
Example:
"aaaXXXbbb".replace(/XXX/, '_before_$<_after_')
Actual result:
"aaa$<_after_bbb"
Expected result:
"aaa_before_$<_after_bbb"
PS: "$$" inserts a "$" and solves this problem
This behavior doesn't occur in Firefox or Chrome, and I think I understand why.
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. 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!
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments