When you addDocumentation 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.
.global() to a TS-Rex builder, you are telling the pattern to scan the entire input and yield every match, not just the first one. TS-Rex models this at the type level: the return type of exec() shifts from the MatchResult discriminated union to an IterableIterator<SingleMatch<TCaptures, TFlags>>. You iterate over it with a standard for...of loop — no index tracking, no lastIndex management.
The digit example
The README’s canonical global example extracts every number from a sentence.results is an IterableIterator. Each yielded value is a SingleMatch object with isMatch: true (always), match: string, and every named capture inferred as string. There is no failure branch inside the iterator — if the underlying regex finds no matches at all the iterator simply yields nothing.
How .global() changes the return type
Without.global(), exec() returns:
.global(), exec() returns:
.global() the compiler will not let you access .isMatch on the outer result — it is an iterator, not a union. The individual items you pull from the iterator do carry isMatch: true and all named captures as non-optional strings.
Stateless execution: no lastIndex bugs
The classic pitfall with nativeRegExp and the g flag is lastIndex. A stateful regex object remembers where it stopped and resumes from that position on the next call. If you reuse the same RegExp instance across multiple exec() calls, you get confusing gaps and missed matches.
TS-Rex eliminates this entirely. Every call to exec() creates a fresh RegExp instance internally. The lastIndex of the native pattern always starts at 0, so calling exec() on the same compiled pattern multiple times on the same string always returns the same full set of matches.
The same stateless guarantee applies to the
y (sticky) flag. Each exec() call starts from index 0 regardless of how many times you have called exec() before.Contrast with native RegExp
Here is how the same operation looks with a rawRegExp and why it trips up developers:
lastIndex to you. The compiled pattern.native property gives you access to the underlying RegExp for inspection, but all actual matching goes through the exec() wrapper which handles fresh instantiation internally.
Combining .global() with .withIndices()
You can layer flags. Adding.withIndices() (the d flag) alongside .global() injects an indices property on each yielded match, giving you the [start, end] tuple for every captured group.
indices property is typed as Record<keyof TCaptures, [number, number]> & { match: [number, number] }, so TypeScript knows exactly which group names are available as index keys.