Skip to main content

Free regular expression cheatsheet. Search across character classes, quantifiers, anchors, groups, alternation, lookaround, character escapes, and flags — each entry shows the syntax, what it matches, and a working pattern + example. Tuned to ECMAScript regex (browser / Node.js).

Character classes

  • .
    Any character
    Matches any single character except newline (unless `s` flag).
    /c.t/matches 'cat', 'cot', 'cut'
  • \d
    Digit
    Matches 0-9.
    /\d{3}/matches three digits in a row
  • \D
    Non-digit
    Matches anything except 0-9.
    /\D+/
  • \w
    Word character
    Matches [A-Za-z0-9_].
    /\w+/
  • \W
    Non-word
    Matches anything except [A-Za-z0-9_].
    /\W+/
  • \s
    Whitespace
    Matches space, tab, newline, etc.
    /\s+/
  • \S
    Non-whitespace
    Matches any non-whitespace character.
    /\S+/
  • [abc]
    Character set
    Matches any of the listed characters.
    /[aeiou]/matches any vowel
  • [^abc]
    Negated set
    Matches anything except the listed characters.
    /[^0-9]/
  • [a-z]
    Range
    Matches any character in the range.
    /[a-zA-Z]+/

Quantifiers

  • *
    Zero or more
    Greedy: as many as possible.
    /a*/matches '', 'a', 'aa', ...
  • +
    One or more
    Greedy: at least one, as many as possible.
    /\d+/
  • ?
    Optional
    Zero or one — makes the previous element optional.
    /colou?r/matches 'color' or 'colour'
  • {n}
    Exactly n
    Matches exactly n occurrences.
    /\d{4}/matches exactly 4 digits
  • {n,}
    n or more
    Matches n or more occurrences.
    /a{2,}/
  • {n,m}
    n to m
    Matches between n and m occurrences (inclusive).
    /\d{2,4}/
  • *? +? ?? {n,m}?
    Lazy quantifier
    Matches as FEW characters as possible. Append `?` to any quantifier.
    /<.+?>/shortest tag, not whole document

Anchors & boundaries

  • ^
    Start of string
    Matches the start of input (or line with `m` flag).
    /^Hello/
  • $
    End of string
    Matches the end of input (or line with `m` flag).
    /world$/
  • \b
    Word boundary
    Position between a word character and a non-word character.
    /\bcat\b/matches 'cat' but not 'cats' or 'concatenate'
  • \B
    Non-word boundary
    Position NOT at a word boundary.
    /\Bing/

Groups & alternation

  • (...)
    Capturing group
    Captures matched text into a numbered group.
    /(\d{4})-(\d{2})/captures year and month
  • (?:...)
    Non-capturing group
    Groups without capturing — keeps the match array clean.
    /(?:cat|dog)s?/
  • (?<name>...)
    Named group
    Captures into a named group, accessible as `match.groups.name`.
    /(?<year>\d{4})/
  • |
    Alternation
    Matches either side. Use parentheses to limit scope.
    /jpg|png|gif/
  • \1, \2
    Backreference
    Matches what capture group N captured earlier.
    /(\w+)\s+\1/matches duplicated words

Lookaround (zero-width)

  • (?=X)
    Lookahead
    Match position followed by X — X is NOT consumed.
    /foo(?=bar)/matches 'foo' only if followed by 'bar'
  • (?!X)
    Negative lookahead
    Match position NOT followed by X.
    /foo(?!bar)/
  • (?<=X)
    Lookbehind
    Match position preceded by X.
    /(?<=\$)\d+/digits preceded by $
  • (?<!X)
    Negative lookbehind
    Match position NOT preceded by X.
    /(?<!ab)c/

Escapes & special chars

  • \.
    Literal dot
    Escape `.`, `*`, `?`, `(`, `)`, `[`, `]`, `{`, `}`, `\`, `|`, `+`, `^`, `$`.
    /\d+\.\d+/
  • \n \t \r
    Newline, tab, return
    Match the literal whitespace character.
    /\n+/
  • \xHH
    Hex escape
    Match a character by 2-digit hex code.
    /\x41/matches 'A'
  • \uHHHH
    Unicode escape (BMP)
    Match a Basic Multilingual Plane character by 4-digit hex.
    /\u00e9/
  • \u{H...}
    Unicode escape (full)
    Match any code point. Requires `u` flag.
    /\u{1F600}/u

Flags

  • g
    Global
    Find all matches, not just the first.
    /cat/g
  • i
    Case-insensitive
    Match regardless of case.
    /hello/i
  • m
    Multiline
    `^` and `$` match start/end of each LINE.
    /^foo/m
  • s
    Dotall
    `.` matches newlines too.
    /.+/s
  • u
    Unicode
    Treat pattern as Unicode; enable `\u{...}`.
    /\p{Letter}/u
  • y
    Sticky
    Match starting at `lastIndex` only — useful for tokenizers.
    /foo/y

Common patterns

  • Email (loose)
    Email-ish
    Pragmatic email match — covers ~99% of valid addresses.
    /^[\w.+-]+@[\w-]+\.[\w.-]+$/
  • URL
    HTTP/HTTPS URL
    Match an http or https URL.
    /https?:\/\/[^\s]+/
  • ISO date
    YYYY-MM-DD
    Match a strict ISO calendar date.
    /^\d{4}-\d{2}-\d{2}$/
  • IPv4
    IPv4 address
    Match a dotted-quad address (no full range check).
    /^(\d{1,3}\.){3}\d{1,3}$/
  • Hex color
    CSS hex color
    Match a 3 or 6-digit hex color including `#`.
    /^#([0-9a-fA-F]{3}){1,2}$/

Use the regex tester to try patterns against sample text.

Frequently Asked Questions

Frequently Asked Questions

References

References and sources

Related Tools

Related Tools