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’s auto-escaping system protects you from accidentally producing malformed patterns. When you call .anyOf('a-z') the library escapes the hyphen, so you get [a\-z] — the literal characters a, -, and z — not the range a through z. This is the correct safe default. In practice, most patterns are better expressed using the composition API. But occasionally a complex character class or a non-standard syntax is genuinely easier to write as a raw string, and for those cases TS-Rex provides two escape hatches.

.rawClass(str)

.rawClass(str) wraps your string in square brackets without applying any escaping. Use it when you need a character class with multiple ranges and special characters that would require many chained .or() calls to express safely.
import { rx } from '@fajarnugraha37/ts-rex';

// Injects [a-zA-Z0-9.-] directly into the AST
const pattern = rx().rawClass('a-zA-Z0-9.-').compile();

console.log(pattern.pattern); // [a-zA-Z0-9.-]
The generated pattern is [a-zA-Z0-9.-] — exactly what you wrote, verbatim.
.rawClass() and .raw() bypass all of TS-Rex’s syntactic safety checks. If you pass an invalid or malformed string, the resulting RegExp constructor will throw at runtime. Validate your input carefully, or prefer the safe composition API whenever possible.

.raw(str)

.raw(str) injects any regex string directly into the AST, without brackets and without escaping. Use it to embed patterns that have no corresponding builder method, or to include pre-validated regex fragments from an external source.
import { rx } from '@fajarnugraha37/ts-rex';

// Inject a raw named group with alternation
const pattern = rx().raw('(?<custom>a|b)+').compile();

console.log(pattern.pattern); // (?<custom>a|b)+
The string is placed into the AST as-is. You can combine .raw() with typed builder methods — the AST nodes are concatenated in order.
import { rx } from '@fajarnugraha37/ts-rex';

const pattern = rx()
  .startOfInput()
  .raw('[A-Z]')       // raw uppercase letter class
  .oneOrMore(rx().digit())
  .endOfInput()
  .compile();

console.log(pattern.pattern); // ^[A-Z](?:\d)+$

The safe alternative

Before reaching for an escape hatch, try composing the same pattern using the typed API. For the [a-zA-Z0-9.-] example, the safe equivalent is:
import { rx } from '@fajarnugraha37/ts-rex';

// Safe, typed composition — no raw injection
const myClass = rx()
  .range('a', 'z')
  .or(rx().range('A', 'Z'))
  .or(rx().range('0', '9'))
  .or(rx().anyOf('.-'));

// Compiles to: (?:(?:(?:[a-z]|[A-Z])|[0-9])|[.\-])
The compiled pattern is more verbose than [a-zA-Z0-9.-], but it is semantically identical in every regex engine. The TS-Rex compiler handles the grouping and escaping for you, and you get full type safety throughout.
The verbose alternation form (?:(?:[a-z]|[A-Z])|[0-9]) behaves 100% identically to [a-zA-Z0-9] in JavaScript regex engines. Prefer it unless the verbosity meaningfully harms readability in your specific case.

When .rawClass() is appropriate

.rawClass() is a reasonable choice when the character class is complex, well-understood, and validated — for example, a well-known character set you are copying from a specification or RFC.
import { rx } from '@fajarnugraha37/ts-rex';

// RFC 3986 unreserved characters for URI path segments
// Well-known, validated pattern — rawClass is acceptable here
const unreserved = rx().rawClass('A-Za-z0-9\\-._~');

const pathSegment = rx()
  .capture('segment', rx().oneOrMore(unreserved))
  .compile();
Use your judgment: if you can express the pattern with the safe API in a readable way, do that. If the resulting chain of .or() calls is harder to understand than the equivalent character class string, .rawClass() is an acceptable trade-off — as long as you own the string and can guarantee its correctness.

Decision guide

1

Try the safe composition API first

Use .range(), .anyOf(), .noneOf(), and .or() to build your character class. This is always the preferred approach.
2

Reach for .rawClass() when composition is impractical

If the character class is well-known, externally specified, and tested, .rawClass() avoids unwieldy chains. Double-check the string for correctness — no auto-escaping will save you.
3

Use .raw() only for unsupported syntax

If you need regex syntax that has no corresponding builder method, .raw() lets you inject it. Comment the raw string clearly so future maintainers understand what it does.

Build docs developers (and LLMs) love