Skip to content

Commit 076f950

Browse files
committed
Avoid inserting (?:) to replace whitespace/comments at start of lookbehind group, plus code cleanup
1 parent 3bd1711 commit 076f950

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

src/xregexp.js

+14-19
Original file line numberDiff line numberDiff line change
@@ -242,34 +242,29 @@ function dec(hex) {
242242
* @returns {string} Either '' or '(?:)', depending on which is needed in the context of the match.
243243
*/
244244
function getContextualTokenSeparator(match, scope, flags) {
245+
const matchEndPos = match.index + match[0].length;
246+
const precedingChar = match.input[match.index - 1];
247+
const followingChar = match.input[matchEndPos];
245248
if (
246249
// No need to separate tokens if at the beginning or end of a group
247-
match.input[match.index - 1] === '(' ||
248-
match.input[match.index + match[0].length] === ')' ||
249-
250+
precedingChar === '(' ||
251+
followingChar === ')' ||
250252
// No need to separate tokens if before or after a `|`
251-
match.input[match.index - 1] === '|' ||
252-
match.input[match.index + match[0].length] === '|' ||
253-
253+
precedingChar === '|' ||
254+
followingChar === '|' ||
254255
// No need to separate tokens if at the beginning or end of the pattern
255-
match.index < 1 ||
256-
match.index + match[0].length >= match.input.length ||
257-
258-
// No need to separate tokens if at the beginning of a noncapturing group or lookahead.
259-
// The way this is written relies on:
260-
// - The search regex matching only 3-char strings.
261-
// - Although `substr` gives chars from the end of the string if given a negative index,
262-
// the resulting substring will be too short to match. Ex: `'abcd'.substr(-1, 3) === 'd'`
263-
nativ.test.call(/^\(\?[:=!]/, match.input.substr(match.index - 3, 3)) ||
264-
256+
match.index === 0 ||
257+
matchEndPos === match.input.length ||
258+
// No need to separate tokens if at the beginning of a noncapturing group or lookaround
259+
nativ.test.call(/\(\?(?:[:=!]|<[=!])$/, match.input.substring(0, match.index)) ||
265260
// Avoid separating tokens when the following token is a quantifier
266-
isQuantifierNext(match.input, match.index + match[0].length, flags)
261+
isQuantifierNext(match.input, matchEndPos, flags)
267262
) {
268263
return '';
269264
}
270265
// Keep tokens separated. This avoids e.g. inadvertedly changing `\1 1` or `\1(?#)1` to `\11`.
271-
// This also ensures all tokens remain as discrete atoms, e.g. it avoids converting the syntax
272-
// error `(? :` into `(?:`.
266+
// This also ensures all tokens remain as discrete atoms, e.g. it prevents converting the
267+
// syntax error `(? :` into `(?:`.
273268
return '(?:)';
274269
}
275270

0 commit comments

Comments
 (0)