Quantifiers control how many times a pattern is repeated. In TS-Rex, every quantifier wraps a nestedDocumentation 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.
RegexBuilder — you build the repeatable sub-pattern using the same fluent API, then pass it to the quantifier method. This design lets TypeScript track the optionality of named captures at the type level without any runtime overhead.
When a quantifier allows zero occurrences (
optional, zeroOrMore, or atLeast(0, ...) / between(0, ..., ...)), any named captures inside the wrapped builder are automatically widened to string | undefined in the result type. This reflects the reality that those groups may not participate in the match at all.optional(builder)
(?:...)?.
At the type level, all captures defined inside builder are merged into the outer builder as Partial<InnerCaptures>, meaning each captured group becomes string | undefined.
zeroOrMore(builder)
(?:...)*.
Like optional, all inner captures are typed as Partial<InnerCaptures> because the pattern may match zero times.
oneOrMore(builder)
(?:...)+.
Because the pattern must match at least once, inner captures remain string (not widened to undefined).
times(n, builder)
n times. Maps to (?:...){n}.
n must be a non-negative integer; passing a negative number or a non-integer throws an error. Inner captures are not widened because the pattern always matches the required count.
The exact number of times to repeat the pattern. Must be a non-negative integer.
The sub-pattern to repeat.
atLeast(n, builder)
n times. Maps to (?:...){n,}.
n must be a non-negative integer. When n is 0, inner captures are widened to Partial<InnerCaptures> because zero matches are allowed. When n is 1 or more, captures remain string.
The minimum number of repetitions. Must be a non-negative integer.
The sub-pattern to repeat.
between(min, max, builder)
min and max times (inclusive). Maps to (?:...){min,max}.
min and max must both be non-negative integers, and min must not exceed max. When min is 0, inner captures are widened to Partial<InnerCaptures>.
The minimum number of repetitions. Must be a non-negative integer.
The maximum number of repetitions. Must be a non-negative integer and at least
min.The sub-pattern to repeat.
lazy()
? to the last quantifier chunk in the AST.
Greedy quantifiers consume as many characters as possible and then backtrack. Lazy quantifiers consume as few characters as possible. lazy() must be called directly after a quantifier method (zeroOrMore, oneOrMore, optional, times, atLeast, or between); calling it on an empty builder or after a non-quantifier method throws an error.
lazy() does not take any arguments. It modifies the quantifier already added to the builder chain, so it must immediately follow the quantifier method you want to make non-greedy.Type-level optionality reference
The table below summarises how each quantifier affects the TypeScript type of named captures defined inside the wrapped builder.| Method | Regex | Inner captures type |
|---|---|---|
optional(b) | (?:...)? | Partial<InnerCaptures> (always optional) |
zeroOrMore(b) | (?:...)* | Partial<InnerCaptures> (always optional) |
oneOrMore(b) | (?:...)+ | InnerCaptures (always required) |
times(n, b) | (?:...){n} | InnerCaptures (always required) |
atLeast(0, b) | (?:...){0,} | Partial<InnerCaptures> (optional when n=0) |
atLeast(n, b) n ≥ 1 | (?:...){n,} | InnerCaptures (required) |
between(0, m, b) | (?:...){0,m} | Partial<InnerCaptures> (optional when min=0) |
between(n, m, b) n ≥ 1 | (?:...){n,m} | InnerCaptures (required) |