Reference · Regex · Cheatsheet

JavaScript Regex Cheatsheet

The patterns you reach for most — character classes, anchors, flags, lookarounds, and common recipes. Keep this tab open next to your editor.

Core Syntax

Character Classes

  • . any character except newline (or any with s flag)
  • \d digit, \D non-digit
  • \w word character, \W non-word
  • \s whitespace, \S non-whitespace
  • [abc] any of a, b, c · [^abc] none of them · [a-z] range

Quantifiers

  • * zero or more · + one or more · ? optional
  • {n} exactly n · {n,} n or more · {n,m} between n and m
  • Add ? for lazy matching: .*? smallest match

Anchors

  • ^ start of input (or line with m flag)
  • $ end of input (or line with m flag)
  • \b word boundary · \B non-boundary

Groups

  • (abc) capturing group
  • (?:abc) non-capturing group
  • (?<name>abc) named group
  • \1 backreference · $1, $<name> in replacement

Lookarounds

  • (?=abc) positive lookahead
  • (?!abc) negative lookahead
  • (?<=abc) positive lookbehind
  • (?<!abc) negative lookbehind

Flags

  • g global · i case-insensitive · m multiline
  • s dot matches newline · u Unicode · y sticky

Common Recipes

Email (loose)

/^[^\s@]+@[^\s@]+\.[^\s@]+$/

UUID v4

/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i

ISO date (YYYY-MM-DD)

/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/

Slug (URL fragment)

/^[a-z0-9]+(?:-[a-z0-9]+)*$/

HEX color

/^#(?:[0-9a-f]{3}){1,2}$/i

Try It

Test any of these patterns in our free Regex Tester with live match highlighting and capture-group inspection.

Fail fast

Anchor patterns with ^ and $ when you need a full-string match.

Name your groups

Named groups beat $1/$2 for anything you'll re-read later.

Validate at the edge

Regex is for shape, not business rules — parse, don't validate.