Skip to content

Commit

Permalink
🐛 (conditions) Parse regex flags as well
Browse files Browse the repository at this point in the history
Closes #1393
  • Loading branch information
baptisteArno committed Mar 29, 2024
1 parent 76e7fbd commit a0ba8c5
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/bot-engine/blocks/logic/condition/executeCondition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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] }
}

0 comments on commit a0ba8c5

Please sign in to comment.