Skip to content

Commit

Permalink
fix: regex should match both single and doubles quoted rhs
Browse files Browse the repository at this point in the history
  • Loading branch information
ANGkeith committed Nov 21, 2024
1 parent d5330bd commit 38c592e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ as rhs contains unescaped \`/\``);
return `.match(new RE2(${_rhs}, "${flags}")) ${_operator} null${remainingTokens}`;
});

// Scenario when RHS is surrounded by double-quotes
// Scenario when RHS is surrounded by single/double-quotes
// https://regexr.com/85t0g
const pattern2 = /\s*(?<operator>(?:=~)|(?:!~))\s*"(?<rhs>[^"\\]*(?:\\.[^"\\]*)*)"/g;
evalStr = evalStr.replace(pattern2, (_, operator, rhs) => {
const pattern2 = /\s*(?<operator>(?:=~)|(?:!~))\s*(?<quote_type>["'])(?<rhs>(?:\\.|[^\\])*?)\2/g;
evalStr = evalStr.replace(pattern2, (_, operator, _quote_type, rhs) => {
let _operator;
switch (operator) {
case "=~":
Expand Down
4 changes: 4 additions & 0 deletions tests/rules-regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ const tests = [
rule: '"23" =~ "1234"',
expectedErrSubStr: "must be a regex pattern. Do not rely on this behavior!",
},
{
rule: '"23" =~ \'1234\'',
expectedErrSubStr: "must be a regex pattern. Do not rely on this behavior!",
},
{
rule: '"23" =~ /1234/',
jsExpression: '"23".match(new RE2("1234", "")) != null',
Expand Down

0 comments on commit 38c592e

Please sign in to comment.