TS-Rex turns regex construction into a sequence of typed method calls. You chain builder methods to describe your pattern, callDocumentation 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() to get a typed execution wrapper, and then call .exec() on a string to get a result object whose properties TypeScript already knows. This page walks you through the full workflow from installation to your first match.
Install the package
Add TS-Rex to your project using your preferred package manager:TS-Rex requires TypeScript 5.0 or higher. See the installation page for all package managers and module format details.
Import the factory function
Import
rx from the package. This is the only import you need to start building patterns.Chain builder methods
Call The two
rx() to create a fresh builder, then chain methods to describe your pattern. Each method returns a new immutable builder carrying the accumulated type state..capture() calls tell TypeScript that the result will have firstName and lastName properties of type string.Global iteration
The.global() flag changes the return type of .exec() from a single result object to an IterableIterator. TS-Rex creates a fresh RegExp instance for every execution, so there are no lastIndex mutation bugs to worry about.
When
.global() is set, .exec() always returns an IterableIterator — TypeScript reflects this in the return type automatically based on your builder chain.Match indices
The.withIndices() flag (the ECMAScript d flag) adds an indices property to each match result. Each entry contains a [start, end] tuple for the full match and for every named capture group.
Alternation and union types
The.or() method matches either the pattern built so far or the pattern you pass in. At the type level, it resolves to a union — TypeScript enforces that exactly one branch matched, so capture properties from the other branch are typed as string | undefined.
Next steps
API reference
Browse every builder method with full signatures and examples.
URL parser example
A real-world pattern combining captures, optionality, and character classes.
Core concepts
Understand the AST engine, immutability, and phantom type state in depth.
Escape hatches
When and how to use
.raw() and .rawClass() for advanced patterns.