tracks lexicons and how many times they appeared on the jetstream
1export const createRegexFilter = (pattern: string): RegExp | null => {
2 if (!pattern) return null;
3
4 try {
5 // Check if pattern contains regex metacharacters
6 const hasRegexChars = /[.*+?^${}()|[\]\\]/.test(pattern);
7
8 if (hasRegexChars) {
9 // Use as regex with case-insensitive flag
10 return new RegExp(pattern, "i");
11 } else {
12 // Smart case: case-insensitive unless pattern has uppercase
13 const hasUppercase = /[A-Z]/.test(pattern);
14 const flags = hasUppercase ? "" : "i";
15 // Escape the pattern for literal matching
16 const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
17 return new RegExp(escapedPattern, flags);
18 }
19 } catch (e) {
20 // Invalid regex, return null
21 return null;
22 }
23};