fix(redis): keep user wildcards in fuzzy key search

Refs: #9012
This commit is contained in:
Tara authored and GitHub committed 2026-09-16 20:41:50 +08:00
1 parent 5b60c2efcd
commit 9cb6b28a0f
2 files changed
+66 -3

No files matched your search

+11 -2
View File
@@ -1,8 +1,17 @@
// Full glob metacharacter set — escaped when a value must match literally
// (group subtree patterns, non-fuzzy text).
const REDIS_GLOB_SPECIAL_CHARS = /[\\*?[\]]/g;
const REDIS_GLOB_SPECIAL_CHARS_FUZZY = /[\\*?[\]]/g;
// In fuzzy mode the user's `*` / `?` are intentional wildcards ("search keys
// containing this text"), so only `[` / `]` / `\` are escaped to avoid
// accidental character-class / escape ambiguity. The outer `*…*` wrap added
// by `redisKeySearchPattern` keeps the substring-contains contract, so inputs
// like `prod:*` or `2026*` match the same keys a SCAN with that glob would.
// (#9012: previously `*` was escaped too, so `prod:*` became a literal-`*`
// substring match and found nothing.)
const REDIS_GLOB_LITERAL_CHARS_FUZZY = /[\\[\]]/g;
export function escapeRedisGlobText(value: string, fuzzy = false): string {
return value.replace(fuzzy ? REDIS_GLOB_SPECIAL_CHARS_FUZZY : REDIS_GLOB_SPECIAL_CHARS, "\\$&");
return value.replace(fuzzy ? REDIS_GLOB_LITERAL_CHARS_FUZZY : REDIS_GLOB_SPECIAL_CHARS, "\\$&");
}
export function redisKeySearchPattern(value: string, fuzzy: boolean): string {