# Regular expressions

Source: https://codewiki.com/cheatsheets/regex/

## Literal text

- `/cat/` — match the literal sequence `cat` anywhere in the input
- `/a\.b/` — escape the dot so it matches a literal `.`
- `/https?:\/\//` — match `http://` or `https://`
- `/\n/` — match one line-feed character
- `/\x41/` — match `A` through a two-digit hexadecimal escape
- `/\u{1F600}/u` — match code point `U+1F600` in Unicode mode

## Character sets

- `/[abc]/` — match one listed UTF-16 code unit
- `/[^abc]/` — match one UTF-16 code unit not in the set
- `/[a-z]/` — match one lowercase ASCII letter
- `/\d/` — match one ASCII digit
- `/\w/` — match one ASCII letter, digit, or underscore
- `/\s/` — match one ECMAScript whitespace or line-terminator character
- `/\p{Letter}/u` — match one Unicode code point with the `Letter` property

## Quantifiers

- `/a?/` — match zero or one `a`, preferring one
- `/a*/` — match zero or more `a` characters greedily
- `/a+/` — match one or more `a` characters greedily
- `/a{3}/` — match exactly three `a` characters
- `/a{2,}/` — match at least two `a` characters
- `/a{2,4}/` — match two through four `a` characters, preferring four
- `/a+?/` — match as few `a` characters as the remaining pattern allows

## Positions and boundaries

- `/^text/` — match `text` only at the start of the input
- `/text$/` — match `text` at the input end or before a final line terminator
- `/^text/m` — match `text` at the input start or after a line terminator
- `/text$/m` — match `text` at the input end or before a line terminator
- `/\bword\b/` — require ASCII word boundaries around `word`
- `/\Bword/` — require no ASCII word boundary before `word`

## Groups and backreferences

- `/(cat|dog)/` — choose either alternative and capture it as group 1
- `/(?:cat|dog)/` — group alternatives without creating a capture
- `/(?<kind>cat|dog)/` — capture the chosen alternative as `groups.kind`
- `/((a)b)/` — number nested captures by their opening parentheses
- `/([a-z]+)-\1/` — require the text after `-` to repeat group 1 exactly
- `/(?<word>[a-z]+)\s+\k<word>/i` — find a repeated ASCII word with a named backreference

## Lookaround

- `/q(?=u)/` — match `q` only when `u` follows, without consuming `u`
- `/q(?!u)/` — match `q` only when `u` does not follow
- `/(?<=\$)\d+/` — match digits preceded by `$` without consuming `$`
- `/(?<!\$)\b\d+/` — match ASCII digits not immediately preceded by `$`
- `/^(?!.*forbidden).*$/s` — reject any input containing the literal text `forbidden`
- `/(?<!\w)cat(?!\w)/` — match `cat` outside adjacent ASCII word characters

## Flags

- `/cat/i` — match letters without case sensitivity
- `/^cat$/m` — make `^` and `$` recognize line boundaries
- `/a.b/s` — let dot match line terminators
- `/cat/g` — enable successive matches and stateful `lastIndex`
- `/cat/d` — include start-end index pairs in match results
- `/\p{Script=Greek}+/u` — enable Unicode matching and property escapes
- `/[\p{Letter}&&\p{Script=Greek}]+/v` — use Unicode Sets mode to intersect two properties

## Match results

- `/cat/.test(text)` — return a boolean indicating whether a match exists
- `/(?<id>\d+)/.exec(text)` — return the first detailed match with a named capture, or `null`
- `text.match(/\d+/)` — return the first detailed match, or `null`
- `text.match(/\d+/g)` — return all full matches without capture details, or `null`
- `[...text.matchAll(/(?<id>\d+)/g)]` — collect detailed results for every match
- `match.groups.id` — read the value of the named capture `id`
- `match.indices[0]` — read the full match's `[start, end]` pair when using `d`

## Search, split, and replace

- `text.search(/error/i)` — return the first match index, or `-1`
- `text.split(/\s*,\s*/)` — split on commas and discard adjacent whitespace
- `text.replace(/\s+/g, ' ')` — replace every whitespace run with one space
- `text.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3/$2/$1')` — reorder three numbered captures in the first match
- `text.replace(/(?<last>\w+), (?<first>\w+)/, '$<first> $<last>')` — reorder named captures in the first match
- `text.replace(/\w+/g, word => word.toUpperCase())` — compute a replacement for each ASCII word

## Construction and state

- `RegExp.escape(input)` — escape runtime text for literal insertion into a pattern
- `new RegExp(RegExp.escape(input), 'u')` — build a Unicode-mode pattern that treats `input` literally
- `pattern.source` — read the pattern text without delimiters or flags
- `pattern.flags` — read enabled flags in canonical order
- `pattern.lastIndex` — read where the next global or sticky match starts
- `pattern.lastIndex = 0` — reset a global or sticky pattern before reuse
- `new RegExp(pattern.source, pattern.flags)` — clone a pattern with `lastIndex` reset to zero

## Text checks

- `/^[A-Za-z][A-Za-z0-9_]*$/` — validate an ASCII identifier in single-line input
- `/^\d+$/` — validate one or more ASCII digits in single-line input
- `/^\p{Decimal_Number}+$/u` — validate Unicode decimal digits in single-line input
- `/^\s*$/u` — test whether the input is empty or entirely whitespace
- `/\r\n|[\n\r\u2028\u2029]/` — match one ECMAScript line-break sequence
- `/[ \t]+$/gm` — find trailing spaces or tabs on each line
- `/\bTODO\b/g` — find `TODO` as a complete ASCII word
