Skip to content

Commit

Permalink
Avoid inserting (?:) before/after a group, and go back to slicing onl…
Browse files Browse the repository at this point in the history
…y the last few chars to look for lookaround
  • Loading branch information
slevithan committed Jan 10, 2021
1 parent 076f950 commit d78a262
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/xregexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,16 @@ function getContextualTokenSeparator(match, scope, flags) {
const precedingChar = match.input[match.index - 1];
const followingChar = match.input[matchEndPos];
if (
// No need to separate tokens if at the beginning or end of a group
precedingChar === '(' ||
followingChar === ')' ||
// No need to separate tokens if before or after a `|`
precedingChar === '|' ||
followingChar === '|' ||
// No need to separate tokens if at the beginning or end of a group, before or after a
// group, or before or after a `|`
/^[()|]$/.test(precedingChar) ||
/^[()|]$/.test(followingChar) ||
// No need to separate tokens if at the beginning or end of the pattern
match.index === 0 ||
matchEndPos === match.input.length ||
// No need to separate tokens if at the beginning of a noncapturing group or lookaround
nativ.test.call(/\(\?(?:[:=!]|<[=!])$/, match.input.substring(0, match.index)) ||
// No need to separate tokens if at the beginning of a noncapturing group or lookaround.
// Looks only at the last 4 chars (at most) for perf when constructing long regexes.
/\(\?(?:[:=!]|<[=!])$/.test(match.input.substring(match.index - 4, match.index)) ||
// Avoid separating tokens when the following token is a quantifier
isQuantifierNext(match.input, matchEndPos, flags)
) {
Expand Down

0 comments on commit d78a262

Please sign in to comment.