Skip to content

Commit cf0ad5d

Browse files
committed
fix RegExp sticky polyfill with alternation
1 parent ad66273 commit cf0ad5d

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- Fixed named backreferences in `RegExp` NCG polyfill
3232
- Fixed some cases of `RegExp` NCG polyfill in combination with other types of groups
3333
- Fixed some cases of `RegExp` NCG polyfill in combination with `dotAll`
34+
- Fixed `RegExp` `sticky` polyfill with alternation
3435
- Fixed handling of some line terminators in case of `multiline` + `sticky` mode in `RegExp` polyfill
3536
- Fixed `.input` slicing on result object with `RegExp` `sticky` mode polyfill
3637
- Fixed handling of empty groups with `global` and `unicode` modes in polyfills

packages/core-js/internals/regexp-exec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ if (PATCH) {
7979
var prevChar = re.lastIndex > 0 && charAt(str, re.lastIndex - 1);
8080
if (re.lastIndex > 0 &&
8181
(!re.multiline || re.multiline && prevChar !== '\n' && prevChar !== '\r' && prevChar !== '\u2028' && prevChar !== '\u2029')) {
82-
source = '(?: ' + source + ')';
82+
source = '(?: (?:' + source + '))';
8383
strCopy = ' ' + strCopy;
8484
charsAdded++;
8585
}

tests/unit-global/es.regexp.exec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,12 @@ if (DESCRIPTORS) {
9494
regex5.lastIndex = 2;
9595
assert.deepEqual(regex5.exec('.\u2029bar'), ['bar'], 'multiline sticky after \\u2029');
9696
});
97+
98+
QUnit.test('RegExp#exec sticky with alternation', assert => {
99+
const re = new RegExp('a|b', 'y');
100+
re.lastIndex = 1;
101+
const result = re.exec('cb');
102+
assert.deepEqual(result, ['b'], 'alternation matches non-first alternative at lastIndex');
103+
assert.same(re.lastIndex, 2, 'lastIndex updated correctly');
104+
});
97105
}

0 commit comments

Comments
 (0)