The Ultimate Regular Expressions (RegEx) Syntax & Patterns Cheat Sheet
Quick reference for character classes, anchors, quantifiers, lookarounds, flags, and tested production regex patterns.
Table of Contents
Character Classes & Shortcuts
Standard shorthand escape codes for matching common character sets.
| Character | Matches | Example Matches |
|---|---|---|
| . | Any character except newline (unless `s` flag is set) | `a`, `9`, `#` |
| \d | Any decimal digit `[0-9]` | `5`, `0`, `8` |
| \D | Any non-digit character `[^0-9]` | `a`, `Z`, `!` |
| \w | Word character: alphanumeric plus underscore `[A-Za-z0-9_]` | `user_name1` |
| \W | Non-word character `[^A-Za-z0-9_]` | `-`, `@`, `.` |
| \s | Whitespace character (space, tab `\t`, newline `\n`) | ` `, `\t`, `\n` |
| \S | Non-whitespace character | Any non-space glyph |
| [abc] | Explicit set: matches `a`, `b`, or `c` | `a` in "cat" |
| [^abc] | Negated set: matches any character EXCEPT `a`, `b`, or `c` | `d`, `x`, `1` |
| [a-z] | Range: matches lowercase characters between `a` and `z` | `k`, `m`, `q` |
Anchors & Boundaries
Specify the position in the string where a match must occur without consuming characters.
| Anchor | Description | Code Snippet |
|---|---|---|
| ^ | Start of string or start of line in multiline mode (`m`) | `^https?://` |
| $ | End of string or end of line in multiline mode (`m`) | `\.pdf$` |
| \b | Word boundary (transition between `\w` and `\W`) | `\bcat\b` matches "cat" but not "catch" |
| \B | Non-word boundary (within a word) | `\Bcat\B` matches inside "scattered" |
Quantifiers & Repetition
Specify how many times a character or subpattern should repeat. Suffix with `?` for non-greedy (lazy) matching.
| Quantifier | Meaning | Greedy vs Lazy |
|---|---|---|
| * | 0 or more times (`{0,}`) | `a*` matches "aaa" fully; `a*?` matches empty string |
| + | 1 or more times (`{1,}`) | `a+` matches all "a"s; `a+?` matches the first "a" |
| ? | 0 or 1 time (optional) | `https?` matches "http" or "https" |
| {n} | Exactly n occurrences | `\d{4}` matches 4 consecutive digits |
| {n,} | At least n occurrences | `\w{3,}` matches 3 or more word chars |
| {n,m} | Between n and m occurrences (inclusive) | `\d{2,4}` matches 2 to 4 digits |
Lookaheads & Lookbehinds (Zero-Width Assertions)
Assert whether a pattern is preceded or followed by another pattern without including it in the match result.
| Syntax | Type | Description & Example |
|---|---|---|
| (?=abc) | Positive Lookahead | Asserts pattern is followed by `abc`. `\d+(?=px)` matches "100" in "100px". |
| (?!abc) | Negative Lookahead | Asserts pattern is NOT followed by `abc`. `\d+(?!px)` matches digits not followed by "px". |
| (?<=abc) | Positive Lookbehind | Asserts pattern is preceded by `abc`. `(?<=\$)\d+` matches "50" in "$50". |
| (?<!abc) | Negative Lookbehind | Asserts pattern is NOT preceded by `abc`. `(?<!\$)\d+` matches numbers without dollar sign. |
Verified Production RegEx Patterns
Tested, copy-ready regex snippets for common real-world software engineering tasks.
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$^[a-z0-9]+(?:-[a-z0-9]+)*$Interactive Client-Side Tools for this Guide
Test and run these patterns directly inside your browser with zero server uploads:
Online RegEx Tester & Syntax Debugger
Test regular expressions against multiline strings in real time. Inspect capture groups, match indices, and flags with instant feedback.
JSON Formatter & Validator (Beautify & Minify)
Clean up messy API responses, fix syntax glitches, and compress JSON payloads directly in your browser.
Frequently Asked Questions
What is the difference between greedy and lazy matching?
By default, regex quantifiers (*, +, {n,}) are greedy: they consume as much text as possible while still allowing the pattern to match. Adding a question mark (?) turns them lazy (or non-greedy), causing them to consume as few characters as possible.
How do I test my regex patterns safely without exposing sensitive data?
Use DocSwift's 100% client-side Regex Tester. It runs directly inside your browser’s JavaScript runtime without sending test strings or tokens to any external server.