Regex Cheatsheet

Regular-expression syntax with examples.

About Regex Cheatsheet

A regex cheatsheet of 120 regular expression tokens in 10 sections, from character classes and quantifiers to lookarounds, flags and common patterns.

How to use

  1. Type in the filter box to narrow the tables. The filter matches the token itself or its description, so both \d and "digit" find the same row.
  2. Scroll the sections that remain. Press Escape or Clear to show everything again.
  3. Open the Regex Tester to try a token against your own text.

What it covers

Section Contents
Characters ., \d, \w, \s, sets, ranges, \b, escapes such as \n, \xhh, \u{hhhh}
Character classes POSIX classes such as [:alpha:] and [:digit:]
Unicode \p{L}, \p{N}, \p{Script=Latin} and the negated \P{…}
Anchors ^, $, \A, \Z, \z, \G, \K
Quantifiers greedy, lazy and possessive forms of *, +, ? and {n,m}
Groups and references capturing, non-capturing, named, backreferences, atomic, conditional, recursion
Lookarounds positive and negative lookahead and lookbehind
Flags and modifiers /i, /g, /m, /s, /u, /y, /x and inline (?i) forms
Special constructs backtracking verbs such as (*PRUNE) and comments
Common patterns email, URL, dates, times, hex colour, numbers, US phone and ZIP, card number formats
  • The sheet is a cross-flavour reference. POSIX classes, possessive quantifiers, atomic groups, \A, \z, \K, recursion and the (*…) verbs come from PCRE and do not work in JavaScript, which supports the Characters, Unicode (with the u flag), Quantifiers (greedy and lazy), Lookarounds and the /g /i /m /s /u /y flags.
  • The common patterns are starting points. The email pattern, for example, accepts most addresses but is not a full RFC 5322 validator.

Example

Filter: lookbehind
→ (?<=abc)   Positive lookbehind
→ (?<!abc)   Negative lookbehind
→ (?<=^.{5}) Lookbehind for first 5 chars

Privacy

The reference is part of the page. Filtering runs in the browser, no request is made, and nothing is stored.

Frequently asked questions

What is the difference between \b and \B in regex?
\b matches a word boundary: the position between a word character (letter, digit or underscore) and a non-word character or the string edge. \B matches any position that is not a boundary, so \Bcat\B finds "cat" only inside a longer word.
What is a lookahead in regex?
A zero-width check that the text after the current position matches a sub-pattern without consuming it. (?=…) requires it, (?!…) forbids it. Lookbehind, (?<=…) and (?<!…), checks the text before the position instead.
What is the difference between * and + in regex?
* matches zero or more repetitions of the preceding item, so it can match nothing. + matches one or more, so at least one occurrence is required. ? matches zero or one.
Which regex features are not supported in JavaScript?
Possessive quantifiers (a++), atomic groups (?>…), the \A, \z and \K anchors, inline flags like (?i), the x (free-spacing) flag, recursion and conditional groups. These come from PCRE and are available in PHP, Perl and some Python libraries, not in the browser.