Skip to content
All posts

Complete regex cheatsheet (with copy-paste patterns that actually work)

Every metacharacter, every flag, and 30 real-world patterns for email, IP, date, URL, slug, credit-card and more. All testable in the browser.

DDDev DeskDeveloper Tools EditorPublished April 24, 20268 min readintermediate

# The fundamentals

# Character classes

| Pattern | Matches |

|---|---|

| . | Any character except newline (or anything with /s flag) |

| \d | Digit [0-9] |

| \D | Non-digit |

| \w | Word char [A-Za-z0-9_] |

| \W | Non-word |

| \s | Whitespace (space, tab, newline) |

| \S | Non-whitespace |

| [abc] | Any of a, b, or c |

| [^abc] | Anything except a, b, or c |

| [a-z] | Range |

# Anchors

| Pattern | Matches |

|---|---|

| ^ | Start of string (or line with /m) |

| $ | End of string (or line with /m) |

| \b | Word boundary |

| \B | Non-word-boundary |

# Quantifiers

| Pattern | Matches |

|---|---|

| * | 0 or more |

| + | 1 or more |

| ? | 0 or 1 |

| {n} | Exactly n |

| {n,} | n or more |

| {n,m} | Between n and m |

| *? / +? / ?? | Lazy (match as few as possible) |

# Groups

| Pattern | Meaning |

|---|---|

| (abc) | Capturing group |

| (?:abc) | Non-capturing (faster, no back-reference) |

| (?<name>abc) | Named group |

| \1 | Back-reference to group 1 |

| (?=abc) | Positive lookahead |

| (?!abc) | Negative lookahead |

| (?<=abc) | Positive lookbehind |

| (?<!abc) | Negative lookbehind |

# 30 patterns you'll actually use

<div class="callout callout-tip" role="note"><div class="callout-title">Tip</div><div class="callout-body"><p>Every pattern below is testable in-browser in our <a href="/regex-tester">Regex Tester</a> — paste the pattern and your test string, see matches highlighted live.</p></div></div>

# Numbers

  • Integer: ^-?\d+$
  • Positive integer: ^\d+$
  • Float (with or without sign): ^-?\d+(\.\d+)?$
  • Percentage: ^(100|\d{1,2}(\.\d+)?)%$
  • Currency: ^\$?\d+(,\d{3})*(\.\d{2})?$

# Dates and times

  • ISO 8601 date: ^\d{4}-\d{2}-\d{2}$
  • ISO 8601 datetime: ^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?$
  • Time HH:MM: ^([01]\d|2[0-3]):[0-5]\d$
  • Time HH:MM:SS: ^([01]\d|2[0-3]):[0-5]\d:[0-5]\d$

# Email + web

  • Email (practical): ^[^\s@]+@[^\s@]+\.[^\s@]+$
  • URL: ^https?:\/\/[^\s/$.?#].[^\s]*$
  • Domain: ^(?:a-z0-9?\.)+[a-z]{2,}$
  • Slug: ^[a-z0-9]+(?:-[a-z0-9]+)*$

# IPs

  • IPv4: ^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
  • IPv6 (simple): ^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$

# Identifiers

  • UUID v4: ^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
  • Hex color: ^#(?:[0-9a-fA-F]{3}){1,2}$
  • MAC address: ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$
  • Credit card (any 13–19 digits): ^\d{13,19}$

# Strings

  • Starts with a letter: ^[A-Za-z]
  • Only alphanumeric: ^[A-Za-z0-9]+$
  • Password: 8+ chars, letter + digit: ^(?=.[A-Za-z])(?=.\d).{8,}$
  • Password strong: 12+ chars, mixed case + digit + symbol: ^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[^A-Za-z0-9]).{12,}$

# Code

  • JWT (rough shape): ^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$
  • Hex string: ^[0-9a-fA-F]+$
  • Base64 (with padding): ^[A-Za-z0-9+/]*={0,2}$
  • Markdown link: \[([^\]]+)\]\(([^)]+)\)

# HTML-ish

  • Strip HTML tags (replace with empty): <[^>]+>
  • Extract src attribute: src="'["']
  • Extract first heading: <h1[^>]>([\s\S]?)<\/h1>

# Whitespace

  • Trim whitespace (replace both sides): ^\s+|\s+$
  • Collapse runs of whitespace: \s+ (replace with " ")

# Flags reference

| Flag | Name | Use |

|---|---|---|

| g | Global | Find all matches, not just first |

| i | Ignore case | Case-insensitive |

| m | Multiline | ^ / $ match line ends |

| s | dotAll | . matches newlines |

| u | Unicode | Handle surrogate pairs, \u{10FFFF} |

| y | Sticky | Anchor each match at lastIndex |

# Debugging regex

  • Build in small pieces; add one group at a time.
  • Name your captures: (?<year>\d{4})-(?<month>\d{2}) — reads itself.
  • Catastrophic backtracking: nested quantifiers like (a+)+b can hang on aaaaaaaac. Avoid.
  • When in doubt, paste into our Regex Tester — the live highlighting makes mistakes obvious.

Frequently asked questions

Is this JavaScript regex or something else?

JavaScript (ECMAScript) regex syntax. Same engine your browser runs. Most patterns are portable to Python, Ruby and Go but lookbehinds and Unicode property escapes vary.

Why shouldn't I use regex for HTML or email?

HTML nesting isn't regular — you need a parser. Email's official grammar (RFC 5322) is thousands of characters. The patterns below are *good enough* approximations for 99% of real input.

What do flags like `g`, `i`, `m` do?

`g` = global (find all matches, not just the first). `i` = case-insensitive. `m` = multiline (`^` and `$` match line boundaries, not string boundaries). `s` = dotAll (`.` matches newlines). `u` = unicode (treat surrogate pairs correctly).

Новые записи, раз в неделю.

Практические руководства для разработчиков. Без спама. Отписаться в любое время.

Tools mentioned

Keep reading