Regex Cheat Sheet

Searchable regular expression reference with examples, language notes, and links to try each pattern live.

Quick Reference

Chars
. any
\d digit
\w word
\s space
Quant
* 0+
+ 1+
? 0/1
{n} exact
Anchors
^ start
$ end
\b boundary
Groups
() capture
(?:) non-cap
| or
Classes
[abc] set
[^a] not
[a-z] range
Look
(?=) ahead+
(?!) ahead-
(?<=) behind+

Characters

PatternDescription
.
Any character except newline
Try it
\d
Digit (0-9)
Try it
\D
Non-digit
Try it
\w
Word character (a-z, A-Z, 0-9, _)
Try it
\W
Non-word character
Try it
\s
Whitespace (space, tab, newline)
Try it
\S
Non-whitespace
Try it
\t
Tab character
Try it
\n
Newline character
Try it
\\
Literal backslash
Try it

Quantifiers

PatternDescription
*
0 or more
Try it
+
1 or more
Try it
?
0 or 1 (optional)
Try it
{n}
Exactly n times
Try it
{n,}
n or more times
Try it
{n,m}
Between n and m times
Try it
*?
0 or more (lazy)
Try it
+?
1 or more (lazy)
Try it

Anchors

PatternDescription
^
Start of string/line
Try it
$
End of string/line
Try it
\b
Word boundary
Try it
\B
Non-word boundary
Try it

Groups & References

PatternDescription
(abc)
Capturing group
Try it
(?:abc)
Non-capturing group
Try it
(?<name>abc)
Named capturing group
JS, Python, Java. Go uses (?P<name>)
Try it
\1
Back-reference to group 1
Try it
(a|b)
Alternation (or)
Try it

Lookaround

PatternDescription
(?=abc)
Positive lookahead
Try it
(?!abc)
Negative lookahead
Try it
(?<=abc)
Positive lookbehind
JS (ES2018+), Python, Java. Not in Go
Try it
(?<!abc)
Negative lookbehind
JS (ES2018+), Python, Java. Not in Go
Try it

Character Classes

PatternDescription
[abc]
Any of a, b, or c
Try it
[^abc]
Not a, b, or c
Try it
[a-z]
Range: a to z
Try it
[0-9]
Range: 0 to 9
Try it
[\s\S]
Any character (incl. newline)
Try it

Flags

PatternDescription
g
Global - match all occurrences
JS, Python (re.findall). Go matches all by default
Try it
i
Case-insensitive
All. Python: re.IGNORECASE. Go: (?i)
Try it
m
Multiline - ^ and $ match line boundaries
All. Python: re.MULTILINE. Go: (?m)
Try it
s
Dotall - dot matches newline
JS (ES2018+), Python (re.DOTALL), Java. Go: (?s)
Try it
u
Unicode support
JS, Python (default in 3.x), Java. Go is UTF-8 native
Try it

Common Patterns

PatternDescription
^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$
Email address (basic)
Try it
^https?://[\w.-]+(?:/[\w./-]*)?$
URL (basic HTTP/HTTPS)
Try it
^\d{1,3}(\.\d{1,3}){3}$
IPv4 address (basic)
Try it
^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Hex color code
Try it
^\d{4}-\d{2}-\d{2}$
Date (YYYY-MM-DD)
Try it
^\+?[1-9]\d{1,14}$
Phone number (E.164)
Try it
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{8,}$
Strong password (8+, upper, lower, digit)
Try it
^-?\d+(\.\d+)?$
Number (integer or decimal)
Try it

Language-Specific Notes

JavaScript

  • Regex literals: /pattern/flags
  • Named groups: (?<name>...)
  • Lookbehind supported since ES2018
  • \p{L} Unicode with u flag
  • d flag for indices (ES2022)

Python

  • Use r"raw strings" for patterns
  • Named groups: (?P<name>...)
  • Flags: re.IGNORECASE, re.MULTILINE, re.DOTALL
  • re.findall() returns all matches
  • Verbose mode: re.VERBOSE for commented patterns

Go

  • Uses RE2 syntax (no backtracking)
  • No lookbehind support
  • Named groups: (?P<name>...)
  • Inline flags: (?i), (?m), (?s)
  • Use backticks for raw strings: `\d+`

Java

  • Double-escape in strings: "\\d+"
  • Named groups: (?<name>...)
  • Flags via Pattern.compile(pattern, flags)
  • Possessive quantifiers: *+, ++
  • Atomic groups: (?>...)

What is a Regex Cheat Sheet?

A regex cheat sheet is a quick reference for regular expression syntax — character classes, quantifiers, anchors, groups, lookaheads, and flags. Regex syntax is powerful but dense, and even experienced developers need to look up less common features. This reference covers JavaScript regex syntax with examples and descriptions for every pattern type.

Common Use Cases

  • Quickly referencing regex syntax while writing patterns
  • Looking up quantifiers, character classes, and assertions
  • Finding the correct syntax for lookahead/lookbehind
  • Understanding regex flags (global, multiline, unicode)
  • Learning regex systematically from basics to advanced

Frequently Asked Questions

What is a lookahead in regex?

A lookahead checks if a pattern exists ahead of the current position without consuming characters. Positive lookahead (?=...) matches if the pattern exists; negative lookahead (?!...) matches if it doesn't. Example: \d+(?=px) matches numbers followed by 'px' but doesn't include 'px' in the match.

What regex flags should I know?

The most important: g (global — match all, not just first), i (case-insensitive), m (multiline — ^ and $ match line start/end), s (dotAll — . matches newlines), and u (unicode — correct handling of Unicode characters).