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

Go

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

Fragment

main.go
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

Go

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

Fragment

main.go
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

Go

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.go
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

Go

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.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<.*>      greedy
<.*?>     lazy

Group an expression without capturing it

Go

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

Fragment

main.go
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

Go

| 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.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cat|dog
(?:cat|dog)s?

Read input and line boundaries

Go

^ 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.go
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

Go

\d is ASCII [0-9]. Unicode property classes use \p{...}.

Fragment

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
\d
\p{Nd}

Name a capture

Go

Go 1.22+ accepts (?<name>...) as well as (?P<name>...); names are exposed through SubexpNames and SubexpIndex.

Fragment

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

Recognize capture backreferences

Go

Go regexp does not support pattern backreferences. Compare captured values in application code when equality between captures is required.

Check lookaround support before using it

Go

Go regexp does not support lookahead or lookbehind. Match a larger context and inspect captures, or use application logic.

Select case, line and dot behavior

Go

Inline i, m and s flags control case, multiline anchors and dot/newline behavior.

Fragment

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
(?ims)pattern

Compile or construct a pattern

Go

Compile returns an error for invalid syntax. MustCompile panics and is best reserved for a known program constant.

Fragment

Import regexp. Raw Go strings avoid an extra backslash-escaping layer.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import "regexp"

re, err := regexp.Compile(`[0-9]+`)
if err != nil { return err }

Ask whether any substring matches

Go

MatchString reports whether a match exists anywhere unless the pattern anchors it.

Fragment

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
found := re.MatchString(input)

Match an entire input

Go

\A and \z mean start/end of text, independent of multiline line anchors.

Fragment

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import "regexp"

re := regexp.MustCompile(`\A[A-Z]{2}[0-9]{3}\z`)
valid := re.MatchString(input)

Read captures and missing groups

Go

FindStringSubmatch returns the whole match at index 0 followed by groups, or nil for no match. Use index APIs when unmatched versus empty groups matter.

Fragment

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import "fmt"

parts := re.FindStringSubmatch(input)
if parts != nil { fmt.Println(parts[0]) }

Iterate non-overlapping matches

Go

FindAllString returns non-overlapping matches; n<0 requests all.

Fragment

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
matches := re.FindAllString(input, -1)

Distinguish replacement syntax from literal text

Go

ReplaceAllString interprets $name/${name} references. ReplaceAllLiteralString treats the replacement literally.

Fragment

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
result := re.ReplaceAllLiteralString(input, "$literal")

Quote dynamic input as literal pattern text

Go

QuoteMeta escapes regexp metacharacters in literal input.

Fragment

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import "regexp"

re, err := regexp.Compile(regexp.QuoteMeta(userText))

Choose Unicode properties deliberately

Go

Unicode categories/scripts are supported through \p classes; rune processing does not make grapheme boundaries automatic.

Fragment

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
\p{L}+

Distinguish dot from an arbitrary text parser

Go

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

Fragment

main.go
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

Go

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

Fragment

main.go
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

Go

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

Fragment

main.go
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

Go

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.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
a*
^
(?:)

Choose an engine and input budget that fit

Go

Go regexp guarantees linear-time matching in input length for a fixed compiled pattern. Compilation size and the work you do for all matches still consume resources.

Fragment

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Compile a reusable pattern once; bound input/output sizes.

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

Go

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.go
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

Go

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.go
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