豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit e490caf

Browse files
Fix for #810 (#1514)
Co-authored-by: Denis Pushkarev <zloirock@zloirock.ru>
1 parent 10b4e86 commit e490caf

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- Fixed named backreferences in `RegExp` NCG polyfill
3333
- Fixed some cases of `RegExp` NCG polyfill in combination with other types of groups
3434
- Fixed some cases of `RegExp` NCG polyfill in combination with `dotAll`
35+
- Fixed `String.prototype.replace` with `sticky` polyfill, [#810](https://github.com/zloirock/core-js/issues/810), [#1514](https://github.com/zloirock/core-js/issues/1514)
3536
- Fixed `RegExp` `sticky` polyfill with alternation
3637
- Fixed handling of some line terminators in case of `multiline` + `sticky` mode in `RegExp` polyfill
3738
- Fixed `.input` slicing on result object with `RegExp` `sticky` mode polyfill

packages/core-js/modules/es.string.replace.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,20 @@ fixRegExpWellKnownSymbolLogic('replace', function (_, nativeReplace, maybeCallNa
7676
var rx = anObject(this);
7777
var S = toString(string);
7878

79+
var functionalReplace = isCallable(replaceValue);
80+
if (!functionalReplace) replaceValue = toString(replaceValue);
81+
var flags = toString(getRegExpFlags(rx));
82+
7983
if (
8084
typeof replaceValue == 'string' &&
8185
!~stringIndexOf(replaceValue, UNSAFE_SUBSTITUTE) &&
82-
!~stringIndexOf(replaceValue, '$<')
86+
!~stringIndexOf(replaceValue, '$<') &&
87+
!~stringIndexOf(flags, 'y')
8388
) {
8489
var res = maybeCallNative(nativeReplace, rx, S, replaceValue);
8590
if (res.done) return res.value;
8691
}
8792

88-
var functionalReplace = isCallable(replaceValue);
89-
if (!functionalReplace) replaceValue = toString(replaceValue);
90-
91-
var flags = toString(getRegExpFlags(rx));
9293
var global = !!~stringIndexOf(flags, 'g');
9394
var fullUnicode;
9495
if (global) {

tests/unit-global/es.string.replace.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ const run = assert => {
131131
assert.same('a'.replace(/(.)/, '$0'), '$0');
132132

133133
assert.throws(() => ''.replace.call(Symbol('replace test'), /./, ''), 'throws on symbol context');
134+
135+
assert.same('.a'.replace(new RegExp('a', 'y'), '.'), '.a', 'Replacement for y');
134136
};
135137

136138
QUnit.test('String#replace regression', run);

0 commit comments

Comments
 (0)