diff --git a/src/utils/filter.ts b/src/utils/filter.ts index 63d1dba..e306839 100644 --- a/src/utils/filter.ts +++ b/src/utils/filter.ts @@ -1,9 +1,10 @@ export function matchCharactersWithRegex(word: string, searchTerm: string) { const escapedSearchTerm = searchTerm.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + // Construct a regex pattern that enforces order but allows small gaps const regexPattern = escapedSearchTerm .split("") - .map((char) => `(?=.*${char})`) + .map((char, index) => (index === 0 ? char : `[^${char}]{0,2}${char}`)) // Allow at most 2 non-matching chars between each .join(""); const regex = new RegExp(regexPattern, "i"); // 'i' flag for case-insensitive matching