From a0ba8c5c2a51668350f8a91ee077f61692090356 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Fri, 29 Mar 2024 08:06:16 +0100 Subject: [PATCH] :bug: (conditions) Parse regex flags as well Closes #1393 --- .../blocks/logic/condition/executeCondition.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/bot-engine/blocks/logic/condition/executeCondition.ts b/packages/bot-engine/blocks/logic/condition/executeCondition.ts index ff9290a2f1..90868c92bd 100644 --- a/packages/bot-engine/blocks/logic/condition/executeCondition.ts +++ b/packages/bot-engine/blocks/logic/condition/executeCondition.ts @@ -126,15 +126,18 @@ const executeComparison = case ComparisonOperators.MATCHES_REGEX: { const matchesRegex = (a: string | null, b: string | null) => { if (b === '' || !b || !a) return false - if (b.startsWith('/') && b.endsWith('/')) b = b.slice(1, -1) - return new RegExp(b).test(a) + const regex = preprocessRegex(b) + if (!regex) return false + return new RegExp(regex.pattern, regex.flags).test(a) } return compare(matchesRegex, inputValue, value, 'some') } case ComparisonOperators.NOT_MATCH_REGEX: { const matchesRegex = (a: string | null, b: string | null) => { if (b === '' || !b || !a) return false - return !new RegExp(b).test(a) + const regex = preprocessRegex(b) + if (!regex) return true + return !new RegExp(regex.pattern, regex.flags).test(a) } return compare(matchesRegex, inputValue, value) } @@ -171,3 +174,11 @@ const parseDateOrNumber = (value: string): number => { } return parsed } + +const preprocessRegex = (regex: string) => { + const match = regex.match(/^\/([^\/]+)\/([gimuy]*)$/) + + if (!match) return null + + return { pattern: match[1], flags: match[2] } +}