ADVERT
๐งต Regex Cheatsheet
Anchors, character classes, quantifiers, flags, and real-world regex patterns you can paste into your next search.
Practical regex reference cards for developers and data wranglers. Compare anchors, quantifiers, character classes, and production-ready patterns without leaving your editor.
Anchors & Boundaries
Control where matches begin and end.
| Pattern | Meaning | Usage |
|---|---|---|
^ | Start of string or line | Combine with lookbehinds for advanced validation. |
$ | End of string or line | Always anchor both ends to avoid partial matches. |
\b / \B | Word boundary / non-boundary | Perfect for matching standalone keywords like \bSELECT\b. |
\A / \Z | Absolute start / end (ignore multiline) | Use when multiline mode should not affect anchors. |
Character Classes & Groups
Mix shorthands and custom classes for expressive tokens.
| Pattern | Meaning | Usage |
|---|---|---|
. | Any character except newline | Pair with the s flag to allow newlines (dotall). |
[abc] | Character set | Use ranges like [a-zA-Z] or negate with [^0-9]. |
\d \w \s | Digit, word char, whitespace | Capitalize (\D, \W, \S) to negate the shorthand classes. |
(?:) | Non-capturing group | Group tokens without populating backreferences. |
Quantifiers
Tune how many times a token repeats.
| Pattern | Meaning | Usage |
|---|---|---|
* | 0 or more | Lazy versions add ? (e.g., *?). |
+ | 1 or more | Ideal for required sequences like emails. |
? | 0 or 1 | Use for optional country codes, prefixes, etc. |
{min,max} | Explicit range | Leave max empty for open-ended bounds (e.g., {8,}). |
Lookarounds & Backreferences
Assert context without consuming characters.
| Pattern | Meaning | Usage |
|---|---|---|
(?=...) | Positive lookahead | Ensure following text without consuming it. |
(?!...) | Negative lookahead | Exclude patterns like reserved words. |
(?<=...) | Positive lookbehind | Match text that follows a prefix such as currency symbols. |
\1 \2 | Backreferences | Reuse captured groups for repeated tokens (e.g., HTML tags). |
Flags
Modifiers that change engine behavior.
- g
Global search. Finds all matches instead of the first.
/pattern/g - i
Case-insensitive matching for user-friendly searches.
/pattern/i - m
Multiline anchors treat ^ and $ as line boundaries.
/pattern/m - s
Dotall mode. The dot matches newline characters.
/pattern/s - u
Unicode awareness for astral plane characters.
/pattern/u
Copy-Paste Patterns
Battle-tested snippets for everyday validation.
| Pattern | Meaning | Usage |
|---|---|---|
/^[a-z0-9_-]16$/i | Username (3โ16 chars) | Allow underscores and hyphens without spaces. |
/^\+?[0-9\s-]{7,}$/ | Flexible phone number | Optional +, spaces, or dashes. |
/^[\w.-]+@[\w.-]+\.[a-z]{2,}$/i | Email address | Fast client-side validation (leave RFC edge cases to the backend). |
/^https?:\/\/[\w.-]+/i | HTTP/HTTPS URL | Validate scheme and domain before deeper parsing. |
ADVERT
ADVERT