Reference Entry
RegexpQuery for Term-Level Regular Expressions
Lexical Querying · advanced · order 80
Match indexed terms with JavaScript regular expressions when wildcard patterns are not expressive enough.
Relevant APIs
RegexpQuery for Term-Level Regular Expressions
RegexpQuery matches indexed terms with a JavaScript RegExp or regex pattern string.
Like WildcardQuery, this is term-level behavior. It does not run full-text analysis over the query text.
Basic example
import { DocumentIndex, RegexpQuery, TextFieldIndex } from "@tryformation/querylight-ts";
const index = new DocumentIndex({
title: new TextFieldIndex()
});
index.index({ id: "1", fields: { title: ["querylight"] } });
index.index({ id: "2", fields: { title: ["query planner"] } });
index.index({ id: "3", fields: { title: ["vector search"] } });
const hits = index.searchRequest({
query: new RegexpQuery("title", "^quer")
});
When to use it
- Power-user filters
- Pattern-heavy identifiers
- Cases where
WildcardQueryis too limited
Regexp vs Wildcard
WildcardQueryis simpler and easier to reason aboutRegexpQueryis more expressive
Prefer WildcardQuery if simple * and ? are enough.
Notes
- Matching is done against indexed terms.
- Querylight removes the
gflag from regex objects to avoid stateful repeated-search bugs. - Broad regexes can match many terms, so keep them deliberate.