diff --git a/CHANGELOG.md b/CHANGELOG.md index 98dbac904116..0eff2f91ff4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Changelog ##### Unreleased +- Fixed replacement of substitutes of undefined capture groups in `.replace` in Safari 13.0-, [#471](https://github.com/zloirock/core-js/issues/471), [#745](https://github.com/zloirock/core-js/issues/745), thanks [@mattclough1](https://github.com/mattclough1) - Improved compat data for old engines ##### 3.6.2 - 2020.01.07 diff --git a/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js b/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js index 932469f546ad..eb6e51d0b384 100644 --- a/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js +++ b/packages/core-js/internals/fix-regexp-well-known-symbol-logic.js @@ -87,7 +87,7 @@ module.exports = function (KEY, length, exec, sham) { (KEY === 'replace' && !( REPLACE_SUPPORTS_NAMED_GROUPS && REPLACE_KEEPS_$0 && - REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE + !REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE )) || (KEY === 'split' && !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC) ) { diff --git a/packages/core-js/modules/es.string.replace.js b/packages/core-js/modules/es.string.replace.js index f0e0933ced7f..b11e30652f9a 100644 --- a/packages/core-js/modules/es.string.replace.js +++ b/packages/core-js/modules/es.string.replace.js @@ -20,6 +20,10 @@ var maybeToString = function (it) { // @@replace logic fixRegExpWellKnownSymbolLogic('replace', 2, function (REPLACE, nativeReplace, maybeCallNative, reason) { + var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = reason.REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE; + var REPLACE_KEEPS_$0 = reason.REPLACE_KEEPS_$0; + var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0'; + return [ // `String.prototype.replace` method // https://tc39.github.io/ecma262/#sec-string.prototype.replace @@ -34,8 +38,8 @@ fixRegExpWellKnownSymbolLogic('replace', 2, function (REPLACE, nativeReplace, ma // https://tc39.github.io/ecma262/#sec-regexp.prototype-@@replace function (regexp, replaceValue) { if ( - !reason.REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE && - (reason.REPLACE_KEEPS_$0 || (typeof replaceValue === 'string' && replaceValue.indexOf('$0') === -1)) + (!REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE && REPLACE_KEEPS_$0) || + (typeof replaceValue === 'string' && replaceValue.indexOf(UNSAFE_SUBSTITUTE) === -1) ) { var res = maybeCallNative(nativeReplace, regexp, this, replaceValue); if (res.done) return res.value; diff --git a/tests/compat/tests.js b/tests/compat/tests.js index 4cf54aec10f9..3b30fae1ced4 100644 --- a/tests/compat/tests.js +++ b/tests/compat/tests.js @@ -875,7 +875,11 @@ GLOBAL.tests = { return result; }; - return ''.replace(O) == 7 && execCalled && ''.replace(re2, '$') === '7'; + return ''.replace(O) == 7 + && execCalled + && ''.replace(re2, '$') === '7' + && 'a'.replace(/./, '$0') === '$0' + && /./[Symbol.replace]('a', '$0') === '$0'; }, 'es.string.search': function () { var O = {};