Skip to content
Toolshelf

JavaScript Regex Cheat Sheet

A practical JavaScript regular expression cheat sheet covering flags, groups, anchors, and common patterns — with a link to test them live.

Quick start

In JavaScript, regular expressions are written as /pattern/flags or created with new RegExp(). Test patterns against sample text before shipping them into production validation or parsing code.

Common flags

Flags change matching behavior. Use them deliberately — especially g (global) and m (multiline).

  • g — find all matches, not just the first
  • i — case-insensitive matching
  • m — ^ and $ match line boundaries
  • s — dot (.) also matches newline
  • u — Unicode mode
  • y — sticky matching from lastIndex
  • d — provide match indices (where supported)

Useful building blocks

Anchors and groups help you write precise patterns without accidental partial matches.

  • ^ / $ — start / end of string (or line with m)
  • \d \w \s — digit, word character, whitespace
  • . — any character (except newline unless s)
  • (...) — capturing group
  • (?:...) — non-capturing group
  • (?<name>...) — named capture group
  • ? + * {n,m} — quantifiers

Test before you trust

Regex bugs are easy to miss with hand-picked examples. Use a live tester to inspect matches, capture groups, and replacements against realistic sample text — including edge cases and empty input.

Try these Toolshelf tools

← All articles