From d6d06abe648616c776115439a7bde163327c135b Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Sun, 7 Jul 2024 12:10:18 +0200 Subject: [PATCH] Support strings as regular expressions --- plug-api/lib/query_expression.ts | 3 +++ website/Expression Language.md | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/plug-api/lib/query_expression.ts b/plug-api/lib/query_expression.ts index c309bf04..280d8f65 100644 --- a/plug-api/lib/query_expression.ts +++ b/plug-api/lib/query_expression.ts @@ -259,6 +259,9 @@ function evalSimpleExpression(type: string, val1: any, val2: any, val3: any) { return r.test(val1); } case "!=~": { + if (typeof val2 === "string") { + val2 = [val2, "i"]; + } if (!Array.isArray(val2)) { throw new Error(`Invalid regexp: ${val2}`); } diff --git a/website/Expression Language.md b/website/Expression Language.md index 28314c08..02531153 100644 --- a/website/Expression Language.md +++ b/website/Expression Language.md @@ -87,7 +87,7 @@ Simple boolean expression: {{"pete" = "pete" or "hank" = "pete"}} * `<=` less than or equals, e.g. `age <= 10` * `>` greater than, e.g. `age > 10` * `>=` greater than or equals, e.g. `age >= 10` -* `=~` to match against a regular expression, e.g. `name =~ /^template\//` +* `=~` to match against a regular expression, e.g. `name =~ /^template\//` (the operand can either be a regular expression or a string expression that will be turned into a regular expression) * `!=~` to not match a regular expression, e.g. `name !=~ /^template\//` * `in` member of a list (e.g. `prop in ["foo", "bar"]`) * `+` addition (can also concatenate strings), e.g. `10 + 12` or `name + "!!!"`