From bec91a8a5835b340003381d99ebd5b02596dca4b Mon Sep 17 00:00:00 2001 From: Viren070 Date: Thu, 12 Jun 2025 18:23:00 +0100 Subject: [PATCH] feat: allow passing flags through --- packages/core/src/utils/regex.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/core/src/utils/regex.ts b/packages/core/src/utils/regex.ts index c086e04b..63da2c72 100644 --- a/packages/core/src/utils/regex.ts +++ b/packages/core/src/utils/regex.ts @@ -41,31 +41,41 @@ export async function safeRegexTest( } } +export function parseRegex(pattern: string): { + regex: string; + flags: string; +} { + const regexFormatMatch = /^\/(.+)\/([gimuy]*)$/.exec(pattern); + return regexFormatMatch + ? { regex: regexFormatMatch[1], flags: regexFormatMatch[2] } + : { regex: pattern, flags: '' }; +} + export async function compileRegex( pattern: string, - flags: string = '', bypassCache: boolean = false ): Promise { + const { regex, flags } = parseRegex(pattern); if (bypassCache) { - return new RegExp(pattern, flags); + return new RegExp(regex, flags); } + return await regexCache.wrap( - (p: string, f: string) => new RegExp(p, f), - getSimpleTextHash(`${pattern}|${flags}`), + (p: string, f: string) => new RegExp(p, f || undefined), + getSimpleTextHash(`${regex}|${flags}`), 60, - pattern, + regex, flags ); } export async function formRegexFromKeywords( - keywords: string[], - flags: string = 'i' + keywords: string[] ): Promise { const pattern = `(? filter.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')) .map((filter) => filter.replace(/\s/g, '[ .\\-_]?')) .join('|')})(?=[ \\)\\]_.-]|$)`; - return await compileRegex(pattern, flags); + return await compileRegex(pattern); }