Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Patterns & regex

TypeScript uses JavaScript RegExp; Go uses regexp; Rust uses the optional regex crate; C++ uses std::regex with its default ECMAScript grammar. Pattern rows show regex text; API rows show host-language escaping.

Separate literals from metacharacters

TypeScript

Ordinary characters match themselves. Metacharacters have structural meaning; escape one when its literal form is intended.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cat       matches the sequence cat
a.b       . is a wildcard
a\.b      matches the literal a.b

Choose a character from a class

TypeScript

A bracket class matches one member. A leading ^ inside the class negates it; a hyphen can specify a range.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
[abc]     one of a, b, c
[A-Z]     one ASCII uppercase letter
[^0-9]    one character outside ASCII digits

Repeat the preceding atom

TypeScript

Quantifiers apply to the preceding atom or group. They count matched units under the engine’s character model, not user-perceived grapheme clusters.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
a?        zero or one
a*        zero or more
a+        one or more
a{2,4}    from two to four

Greedy and lazy repetition

TypeScript

A greedy quantifier prefers more input when a match is possible. Appending ? makes it prefer less. Lazy repetition is not a substitute for choosing a correct grammar.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<.*>      greedy
<.*?>     lazy

Group an expression without capturing it

TypeScript

Parentheses group alternatives or repetition and normally capture their matched text. (?:...) groups without assigning a capture number.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
(ab)+       captures the repeated group
(?:ab)+     groups without capturing

Choose among alternatives

TypeScript

| separates alternatives; group them when only part of a pattern should vary. Default alternatives are tried in order, under each engine’s matching policy.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cat|dog
(?:cat|dog)s?

Read input and line boundaries

TypeScript

^ and $ are boundary assertions, not matched characters. Their line behavior depends on multiline flags. Use the engine’s full-match contract when validating an entire input.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
^name$
# Confirm multiline/end-boundary behavior for the named engine.

Know whether digit shorthand includes Unicode

TypeScript

\d matches ASCII digits even in Unicode mode. Use a Unicode property escape when a wider decimal-digit set is intended.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
\d          [0-9]
\p{Decimal_Number}   with u or v mode

Name a capture

TypeScript

Named captures use (?<name>...). Match results expose groups.name.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
(?<word>[A-Za-z]+)

Recognize capture backreferences

TypeScript

A backreference matches the text captured by an earlier group, rather than repeating that group’s pattern.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
([A-Za-z]+)\s+\1

Check lookaround support before using it

TypeScript

Lookahead and modern lookbehind assert surrounding text without consuming it. Check the deployed runtime’s support.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cat(?=s)      followed by s
cat(?!s)      not followed by s
(?<=@)name    preceded by @

Select case, line and dot behavior

TypeScript

i ignores case; m changes line-anchor behavior; s lets dot match line terminators. u and v are distinct Unicode modes and cannot both be selected.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
/pattern/imsu

Compile or construct a pattern

TypeScript

A literal has one regex-escaping layer; a RegExp constructor receives a JavaScript string, adding string escaping.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const literal = /\d+/u;
const dynamic = new RegExp("\\d+", "u");

Ask whether any substring matches

TypeScript

test returns a boolean; global/sticky regexes have mutable lastIndex state.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const found = /[0-9]+/u.test(input);

Match an entire input

TypeScript

Use input-boundary anchors without multiline mode when the complete string must match.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const valid = /^[A-Z]{2}[0-9]{3}$/u.test(input);

Read captures and missing groups

TypeScript

exec returns null or a match object with positional captures and optional named groups.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const match = /(?<name>[A-Za-z]+)/u.exec(input);
if (match) console.log(match.groups?.name);

Iterate non-overlapping matches

TypeScript

matchAll requires a global regex and returns an iterator of match objects.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
for (const match of input.matchAll(/[0-9]+/gu)) {
  console.log(match[0], match.index);
}

Distinguish replacement syntax from literal text

TypeScript

Replacement strings interpret $1, $& and named-group forms. A replacement function’s returned text avoids those string-template substitutions.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const replaced = input.replaceAll(/[0-9]+/gu, () => "$literal");

Quote dynamic input as literal pattern text

TypeScript

RegExp.escape provides standard literal-pattern escaping in supporting modern runtimes. It is not universally available in older browsers/Node versions.

Fragment

TypeScript 6 and later: enable lib: ["ES2025"] for this API (add "DOM" for browser APIs). The emit target can remain ES2023; library declarations do not polyfill runtime features.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const re = new RegExp(RegExp.escape(userText), "u");

Feature-detect or supply a reviewed compatible implementation for older targets. Do not confuse pattern escaping with replacement-string escaping.

Choose Unicode properties deliberately

TypeScript

Unicode property escapes require u or v mode. A property name and script/letter policy are not equivalent to user-language validation.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
/\p{Letter}+/u

Distinguish dot from an arbitrary text parser

TypeScript

Dot’s newline behavior depends on flags and engine semantics. A wildcard pattern does not parse nested or escaped structured data.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
a.b
# Use the engine’s dot-all option when newline matching is intended.

Track pattern, string and transport escaping separately

TypeScript

A regex pattern embedded in a host string and then JSON has multiple encoding layers. Decode each layer before interpreting the next.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Regex intent:        \d+
JSON string value:   "\\d+"
Decoded regex text:  \d+

Check what a word boundary means

TypeScript

\b is defined relative to the engine’s word-character class; Unicode behavior differs. It does not implement natural-language word segmentation.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
\bcat\b
# State the engine/Unicode mode when word boundaries matter.

Handle patterns that can match an empty string

TypeScript

A pattern such as a* can match without consuming input. Use a documented iterator API rather than writing a loop that retries the same empty match forever.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
a*
^
(?:)

Choose an engine and input budget that fit

TypeScript

JavaScript RegExp has no general linear-time guarantee. Ambiguous nested repetition/backreferences on hostile input can consume excessive time. Bound input and choose simpler patterns.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Avoid unbounded ambiguous nesting such as (a+)+$

Use a format-aware parser when the data has a grammar

TypeScript

Regex is useful for bounded token recognition and extraction. JSON, CSV, HTML and nested-language parsing need their format rules, escaping and error handling.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
CSV: a quoted field can contain a comma or newline
JSON: strings can contain escaped delimiters

Separate normalization from matching

TypeScript

Unicode normalization and case policies can change which strings are considered equivalent. Normalize only under an explicit domain contract; matching alone does not establish semantic identity.

Fragment

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
NFC/NFD: canonical normalization forms
Case folding: a separate language/Unicode policy