@@ -161,18 +161,22 @@ export function createRegExpRestore () {
161
161
let m = regExpCache [ '$' + i ] ;
162
162
163
163
// 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
+ }
167
168
// Else find the string in lm and escape & wrap it to capture it
168
169
else {
169
170
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 ) ;
171
176
}
172
177
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
+
176
180
}
177
181
}
178
182
@@ -183,8 +187,9 @@ export function createRegExpRestore () {
183
187
// expressions generated above, because the expression matches the whole
184
188
// match string, so we know each group and each segment between capturing
185
189
// 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 } }` ;
188
193
} ) ;
189
194
190
195
// Create the regular expression that will reconstruct the RegExp properties
0 commit comments