TS-Rex’s auto-escaping system protects you from accidentally producing malformed patterns. When you 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.
.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.
[a-zA-Z0-9.-] — exactly what you wrote, verbatim.
.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.
.raw() with typed builder methods — the AST nodes are concatenated in order.
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:
[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.
.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
Try the safe composition API first
Use
.range(), .anyOf(), .noneOf(), and .or() to build your character class. This is always the preferred approach.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.