Regex Cheat Sheet

MCP

Regex Cheat Sheet

Regex cheat sheet online. Complete regex syntax reference with examples: character classes, quantifiers, anchors, groups, and flags. Free regex guide.

Character Classes

PatternDescription
.Any character except newline
\dAny digit (0-9)
\DAny non-digit
\wWord character (a-z, A-Z, 0-9, _)
\WNon-word character
\sWhitespace (space, tab, newline)
\SNon-whitespace character
[abc]Any character in the set
[^abc]Any character NOT in the set
[a-z]Character range

Quantifiers

PatternDescription
*Match 0 or more times
+Match 1 or more times
?Match 0 or 1 time (optional)
{n}Match exactly n times
{n,}Match n or more times
{n,m}Match between n and m times
*?Lazy * (match as few as possible)
+?Lazy + (match as few as possible)

Anchors

PatternDescription
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNon-word boundary

Groups & Lookarounds

PatternDescription
(...)Capturing group
(?:...)Non-capturing group
(?<name>...)Named capturing group
\1, \2Back-reference to group 1, 2
(?=...)Positive lookahead
(?!...)Negative lookahead
(?<=...)Positive lookbehind
(?<!...)Negative lookbehind
|Alternation (OR)

Flags

g Global

Find all matches, not just the first

i Case Insensitive

Match both upper and lowercase

m Multiline

^ and $ match line start/end, not just string start/end

s Dotall

. matches newline characters too

u Unicode

Enable full Unicode support

y Sticky

Match only at lastIndex position

Common Patterns

Email Basic email validation
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
URL HTTP/HTTPS URLs
https?://[\w.-]+(?:/[\w./-]*)?
Phone (US) US phone numbers
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Date (YYYY-MM-DD) ISO date format
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Date (MM/DD/YYYY) US date format
(?:0[1-9]|1[0-2])/(?:0[1-9]|[12]\d|3[01])/\d{4}
Time (24h) 24-hour time
(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?
IP Address IPv4 address
(?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d)
Hex Color Hex color codes (#fff, #ffffff)
#(?:[0-9a-fA-F]{3}){1,2}
Username Username: 3-20 chars, starts with letter
^[a-zA-Z][a-zA-Z0-9_]{2,19}$
Strong Password Min 8 chars, upper, lower, digit, special
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Credit Card Credit card number format
\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}
Zip Code (US) US zip code (12345 or 12345-6789)
\d{5}(?:-\d{4})?

๐Ÿ’ก Quick Tips

  • 1 Always escape special characters with \\ when matching literals: \\. \\* \\?
  • 2 Use non-capturing groups (?:...) for grouping without capturing
  • 3 Prefer lazy quantifiers *? +? when matching content between delimiters
  • 4 Test your regex with a variety of inputs including edge cases
  • 5 Use \\b word boundaries to match whole words only

Frequently Asked Questions

What is the simplest way to learn regex?

Start with literals and character classes (\d, \w, \s), then add quantifiers (+, *, ?), then anchors (^, $), and finally groups. Practice on real data with the Regex Tester.

Is regex the same across all languages?

The core syntax is similar (PCRE-inspired), but there are differences: Python uses re module with slightly different flags; Go uses RE2 which does not support backreferences; JavaScript does not support POSIX character classes. Always test in the target language.

What is greedy vs lazy matching?

Greedy quantifiers (*, +) match as much as possible. Lazy quantifiers (*?, +?) match as little as possible. Example: on "aXbXc", a.+c (greedy) matches "aXbXc"; a.+?c (lazy) matches "aXbXc" โ€” same here, but the difference is significant when anchors or patterns repeat multiple times.

What are POSIX character classes?

POSIX classes like [:alpha:] or [:digit:] are used in GREP and some other tools but are not supported in JavaScript. Use the equivalent shorthand like \d for digits or [a-zA-Z] for letters instead.

Tips

  • Bookmark the cheatsheet as a companion tab while writing code that uses regex.
  • When confused about a specific token, jump to the Explainer with that pattern to see it decoded visually.
  • The "dot" metacharacter <code>.</code> does NOT match newlines by default โ€” add the <code>s</code> (dotall) flag to change this.
  • Character class negation with <code>[^...]</code> is one of the most useful patterns: <code>[^\s]</code> matches any non-whitespace character.