Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.

Commit 06012a9

Browse files
committed
(bug #231) Initial fix for escaped paren related regex restoration issues
1 parent 9ccd077 commit 06012a9

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/util.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,22 @@ export function createRegExpRestore () {
161161
let m = regExpCache['$'+i];
162162

163163
// If it's empty, add an empty capturing group
164-
if (!m)
165-
lm = '()' + lm;
166-
164+
if (!m) {
165+
arrPush.call(reg, '(');
166+
lm = ')' + lm;
167+
}
167168
// Else find the string in lm and escape & wrap it to capture it
168169
else {
169170
m = m.replace(esc, '\\$&');
170-
lm = lm.replace(m, '(' + m + ')');
171+
let [ left, ...right ] = lm.split(m);
172+
left += '(';
173+
lm = m + ')' + right.join('');
174+
// Push it to the reg and chop lm to make sure further groups come after
175+
arrPush.call(reg, left);
171176
}
172177

173-
// Push it to the reg and chop lm to make sure further groups come after
174-
arrPush.call(reg, lm.slice(0, lm.indexOf('(') + 1));
175-
lm = lm.slice(lm.indexOf('(') + 1);
178+
179+
176180
}
177181
}
178182

@@ -183,8 +187,9 @@ export function createRegExpRestore () {
183187
// expressions generated above, because the expression matches the whole
184188
// match string, so we know each group and each segment between capturing
185189
// groups can be matched by its length alone.
186-
exprStr = exprStr.replace(/(\\\(|\\\)|[^()])+/g, (match) => {
187-
return `[\\s\\S]{${match.replace('\\','').length}}`;
190+
// exprStr = exprStr.replace(/([^\\]\\\(|[^\\]\\\)|[^()])+/g, (match) => {
191+
exprStr = exprStr.replace(/([^\\](\\\\)*\\[\)\(]((\\\\)*\\[\(\)])*|[^()])+/g, (match) => {
192+
return `[\\s\\S]{${match.replace(/\\(.)/g, '$1').length}}`;
188193
});
189194

190195
// Create the regular expression that will reconstruct the RegExp properties

tests/disableregexprestore.js

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ new IntlPolyfill.NumberFormat('en-US', {
2929
assertEqual(RegExp.input, 'a foo test', 'Normally, RegExp.input should be cached and restored');
3030
assertEqual(RegExp.lastMatch, 'foo', 'Normally, RegExp.lastMatch should be cached and restored');
3131

32+
// Issues #231
33+
/function[\s\S]+(})/.exec('function defineProperty\\(\\) \\{\n \\[native code\\]\n\\}');
34+
new IntlPolyfill.NumberFormat('en-US', {
35+
style: 'currency',
36+
currency: 'GBP',
37+
minimumFractionDigits: 2,
38+
});
39+
assertEqual(RegExp.input, 'function defineProperty\\(\\) \\{\n \\[native code\\]\n\\}', 'Normally, RegExp.input should be cached and restored');
40+
assertEqual(RegExp.lastMatch, 'function defineProperty\\(\\) \\{\n \\[native code\\]\n\\}', 'Normally, RegExp.lastMatch should be cached and restored');
41+
3242
IntlPolyfill.__disableRegExpRestore();
3343
/foo/.exec('a foo test');
3444
new IntlPolyfill.NumberFormat('en-US', {

0 commit comments

Comments
 (0)