Documentation Index
Fetch the complete documentation index at: https://mintlify.com/fajarnugraha37/ts-rex/llms.txt
Use this file to discover all available pages before exploring further.
.compile() is the terminal method that turns your builder chain into a typed execution object. Up to this point, every chained call has been building an AST and accumulating type state — no native RegExp is created until .compile() is called. The returned CompiledRegex object carries the fully resolved TCaptures and TFlags types from your chain, giving .exec() a precise return type that reflects every capture group you defined and every flag you set.
The CompiledRegex interface
The raw regex pattern string compiled from the builder’s AST, without flag letters. You can inspect this to verify what pattern was generated or to pass it to another regex engine.
The native JavaScript
RegExp instance created at compile time with the correct flags applied. This instance is exposed for inspection and interop. Do not use it for repeated execution — it retains lastIndex state between calls. Use .exec() instead, which creates a fresh instance on every call.The primary execution method. Accepts an input string and returns a
MatchResult typed according to the captures and flags in your builder chain. Creates a fresh RegExp instance on every call to guarantee stateless execution.Stateless execution guarantee
A well-known pitfall with native JavaScript global and sticky regexes is thatRegExp.prototype.exec mutates lastIndex on the instance. If you call exec on the same instance twice, the second call resumes from where the first left off — a source of subtle bugs when regexes are stored as module-level constants.
TS-Rex eliminates this entirely. On every call to .exec(), a fresh RegExp is instantiated from the stored pattern and flags string:
.exec() is a pure function: the same input always produces the same output, regardless of how many times it has been called before.
MatchResult types
MatchResult is a discriminated union whose shape is determined by TFlags at compile time.
Without the global flag
When global is not set, .exec() returns SingleMatch | FailedMatch. Use the isMatch discriminant to narrow:
SingleMatch<TCaptures, TFlags>
Always
true on a successful match. Use this as the discriminant in if / switch narrowing.The full matched substring.
One property per named capture group defined in the builder chain. All captures are
string on SingleMatch — optionality from .optional(), .zeroOrMore(), or .or() is reflected as string | undefined on the corresponding keys.Present only when
.withIndices() was called. Contains [start, end] tuples for the full match (indices.match) and for each named capture group. Indices follow the same slice convention as String.prototype.slice — start is inclusive, end is exclusive.FailedMatch<TCaptures, TFlags>
Always
false when the pattern did not match.Always
null on a failed match.Every capture key is present but typed as
undefined, allowing safe property access without narrowing first.With the global flag
When .global() is set, MatchResult collapses to IterableIterator<SingleMatch<TCaptures, TFlags>>. There is no FailedMatch — an empty iterator means no matches were found.