Bug 200714 - String.prototype.replace truncates $<number> values
Summary: String.prototype.replace truncates $<number> values
Status: RESOLVED INVALID
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: Safari 12
Hardware: All All
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2019-08-14 07:36 PDT by matt
Modified: 2019-08-19 01:18 PDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description matt 2019-08-14 07:36:27 PDT
# Summary

When replacing with a value that contains $<number>, the value is truncated of $ and first number.

# Test

var string = "Get %{amount} credit";
var regex = new RegExp(`%{amount}`, "g");
console.log(string.replace(regex, "$100"));

# Expected

> Get $100 credit

# Actual

> Get 00 credit
Comment 1 matt 2019-08-14 07:39:08 PDT
This could be related to a non-standard feature: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n#Examples
Comment 2 Radar WebKit Bug Importer 2019-08-14 09:59:13 PDT
<rdar://problem/54306164>
Comment 3 matt 2019-08-19 01:18:27 PDT
After further investigation, this issue looks to be caused by an old String.prototype.replace polyfill from core-js@2.0.3.

Ref: https://github.com/zloirock/core-js/blob/v2.0.3/modules/es6.regexp.replace.js