Skip to content

Commit

Permalink
Allow = inside regexes
Browse files Browse the repository at this point in the history
A fix for something else introduced a potential issue that when
regular expressions have a `=` inside them, they wouldn't be
recognized as such.

This should make the regex detection more robust by checking
the surrounding code, instead of limiting what can be inside.
  • Loading branch information
matthiasmullie committed Sep 13, 2017
1 parent 69f3d7b commit 6e9d575
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,16 @@ protected function extractRegex()
return $placeholder;
};

$pattern = '\/[^=]*?(?<!\\\\)(\\\\\\\\)*\/[gimy]*(?![0-9a-zA-Z\/])';
$pattern = '\/.*?(?<!\\\\)(\\\\\\\\)*\/[gimy]*(?![0-9a-zA-Z\/])';

// a regular expression can only be followed by a few operators or some
// of the RegExp methods (a `\` followed by a variable or value is
// likely part of a division, not a regex)
$keywords = $this->getKeywordsForRegex($this->keywordsReserved, '/');
$before = '([=:,;\)\}\(\{]|^|'.implode('|', $keywords).')\s*';
$after = '[\.,;\)\}]';
$methods = '\.(exec|test|match|search|replace|split)\(';
$this->registerPattern('/'.$pattern.'(?=\s*('.$after.'|'.$methods.'))/', $callback);
$this->registerPattern('/'.$before.'\K'.$pattern.'(?=\s*('.$after.'|'.$methods.'))/', $callback);

// 1 more edge case: a regex can be followed by a lot more operators or
// keywords if there's a newline (ASI) in between, where the operator
Expand Down

0 comments on commit 6e9d575

Please sign in to comment.