Skip to main content

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.

TS-Rex lets you construct complex regular expressions through a chainable TypeScript API — and automatically infers the exact shape of named capturing groups at compile time. No more string | undefined surprises, no more lastIndex mutation bugs, no more unreadable raw regex strings.

Quickstart

Build your first type-safe regex pattern in under five minutes.

Installation

Install TS-Rex via npm, pnpm, bun, or jsr.

Core Concepts

Understand the AST engine, immutability, and phantom type state.

API Reference

Browse every builder method with signatures and examples.

Why TS-Rex?

Native JavaScript RegExp is powerful but fragile. Regex patterns are opaque strings — one typo silently breaks your logic. Named capture groups require manual type casting. Global flag mutations cause subtle lastIndex bugs across executions. TS-Rex solves all three problems.

Static type inference

Named captures are inferred directly from your builder chain — no type assertions needed.

Stateless execution

Fresh RegExp instances on every .exec() call prevent lastIndex mutation bugs entirely.

Automatic escaping

.literal() and .anyOf() auto-escape special characters so your patterns are always safe.

Zero dependencies

Built entirely on standard TypeScript and native RegExp — nothing extra to install.

Get started in 3 steps

1

Install the package

npm install @fajarnugraha37/ts-rex
2

Build a pattern

import { rx } from '@fajarnugraha37/ts-rex';

const pattern = rx()
  .capture('firstName', rx().oneOrMore(rx().wordChar()))
  .whitespace()
  .capture('lastName', rx().oneOrMore(rx().wordChar()))
  .compile();
3

Execute with full type safety

const result = pattern.exec('John Doe');

if (result.isMatch) {
  console.log(result.firstName); // "John" — typed as string
  console.log(result.lastName);  // "Doe"  — typed as string
}

Explore the docs

Character classes

.digit(), .wordChar(), .anyOf(), .range(), and more.

Quantifiers

.optional(), .zeroOrMore(), .oneOrMore(), .times(), and lazy variants.

Groups & alternation

Named captures, non-capturing groups, backreferences, and .or().

Flags

Global iteration, case-insensitive, match indices, Unicode sets, and more.

URL parser example

A real-world example combining captures, optionality, and character classes.

Escape hatches

.raw() and .rawClass() for power users who need full control.

Build docs developers (and LLMs) love