Separate literals from metacharacters
#Ordinary characters match themselves. Metacharacters have structural meaning; escape one when its literal form is intended.
Fragment
Reference
Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.
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.
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.
TypeScript docs MDN regular expressions guide ↗
Ordinary characters match themselves. Metacharacters have structural meaning; escape one when its literal form is intended.
Fragment
A bracket class matches one member. A leading ^ inside the class negates it; a hyphen can specify a range.
Fragment
Quantifiers apply to the preceding atom or group. They count matched units under the engine’s character model, not user-perceived grapheme clusters.
Fragment
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
Parentheses group alternatives or repetition and normally capture their matched text. (?:...) groups without assigning a capture number.
Fragment
| 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
^ 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
\d matches ASCII digits even in Unicode mode. Use a Unicode property escape when a wider decimal-digit set is intended.
Fragment
Named captures use (?<name>...). Match results expose groups.name.
Fragment
A backreference matches the text captured by an earlier group, rather than repeating that group’s pattern.
Fragment
Lookahead and modern lookbehind assert surrounding text without consuming it. Check the deployed runtime’s support.
Fragment
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
A literal has one regex-escaping layer; a RegExp constructor receives a JavaScript string, adding string escaping.
Fragment
test returns a boolean; global/sticky regexes have mutable lastIndex state.
Fragment
Use input-boundary anchors without multiline mode when the complete string must match.
Fragment
exec returns null or a match object with positional captures and optional named groups.
Fragment
matchAll requires a global regex and returns an iterator of match objects.
Fragment
Replacement strings interpret $1, $& and named-group forms. A replacement function’s returned text avoids those string-template substitutions.
Fragment
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.
Feature-detect or supply a reviewed compatible implementation for older targets. Do not confuse pattern escaping with replacement-string escaping.
Unicode property escapes require u or v mode. A property name and script/letter policy are not equivalent to user-language validation.
Fragment
Dot’s newline behavior depends on flags and engine semantics. A wildcard pattern does not parse nested or escaped structured data.
Fragment
A regex pattern embedded in a host string and then JSON has multiple encoding layers. Decode each layer before interpreting the next.
Fragment
\b is defined relative to the engine’s word-character class; Unicode behavior differs. It does not implement natural-language word segmentation.
Fragment
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
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
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
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