Skip to content

Commit 88f6547

Browse files
committed
fix handleHCG \\k corruption
1 parent f53945d commit 88f6547

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

packages/core-js/modules/es.regexp.constructor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ var handleNCG = function (string) {
8989
chr = charAt(string, index);
9090
if (chr === '\\') {
9191
chr += charAt(string, ++index);
92+
// use `\x5c` for escaped backslash to avoid corruption by `\k<name>` to `\N` replacement below
93+
if (!ncg && charAt(chr, 1) === '\\') {
94+
result += '\\x5c';
95+
continue;
96+
}
9297
} else if (chr === ']') {
9398
brackets = false;
9499
} else if (!brackets) switch (true) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,10 @@ if (DESCRIPTORS) {
125125
assert.same(RegExp('(?<year>\\d{4})-\\k<year>').exec('2024-2024')?.[0], '2024-2024', 'NCG \\k backreference #1');
126126
assert.same(RegExp('(?<year>\\d{4})-\\k<year>').exec('2024-2025'), null, 'NCG \\k backreference #2');
127127
assert.same(RegExp('(?<a>.)(?<b>.)\\k<b>\\k<a>').exec('abba')?.[0], 'abba', 'NCG \\k multiple backreferences');
128+
129+
// escaped backslash before `k<name>` should not be treated as backreference
130+
// eslint-disable-next-line regexp/no-unused-capturing-group -- required for testing
131+
assert.same(RegExp('(?<a>x)\\\\k<a>').exec('x\\k<a>')?.[0], 'x\\k<a>', 'NCG \\\\k not confused with \\k backreference');
132+
assert.same(RegExp('(?<a>x)\\\\\\k<a>').exec('x\\x')?.[0], 'x\\x', 'NCG escaped backslash before backreference');
128133
});
129134
}

0 commit comments

Comments
 (0)