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
| Pattern | Description | Example | |
|---|---|---|---|
. | Any character except newline | ||
\d | Any digit (0-9) | ||
\D | Any non-digit | ||
\w | Word character (a-z, A-Z, 0-9, _) | ||
\W | Non-word character | ||
\s | Whitespace (space, tab, newline) | ||
\S | Non-whitespace character | ||
[abc] | Any character in the set | ||
[^abc] | Any character NOT in the set | ||
[a-z] | Character range |
๐ข Quantifiers
| Pattern | Description | Example | |
|---|---|---|---|
* | 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
| Pattern | Description | Example | |
|---|---|---|---|
^ | Start of string (or line with m flag) | ||
$ | End of string (or line with m flag) | ||
\b | Word boundary | ||
\B | Non-word boundary |
๐ฆ Groups & Lookarounds
| Pattern | Description | Example | |
|---|---|---|---|
(...) | Capturing group | ||
(?:...) | Non-capturing group | ||
(?<name>...) | Named capturing group | ||
\1, \2 | Back-reference to group 1, 2 | ||
(?=...) | Positive lookahead | ||
(?!...) | Negative lookahead | ||
(?<=...) | Positive lookbehind | ||
(?<!...) | Negative lookbehind | ||
| | Alternation (OR) |
๐ฉ Flags
g GlobalFind all matches, not just the first
i Case InsensitiveMatch both upper and lowercase
m Multiline^ and $ match line start/end, not just string start/end
s Dotall. matches newline characters too
u UnicodeEnable full Unicode support
y StickyMatch only at lastIndex position
โญ Common Patterns
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}https?://[\w.-]+(?:/[\w./-]*)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])(?:0[1-9]|1[0-2])/(?:0[1-9]|[12]\d|3[01])/\d{4}(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?(?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d)#(?:[0-9a-fA-F]{3}){1,2}^[a-zA-Z][a-zA-Z0-9_]{2,19}$^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\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
\\bword boundaries to match whole words only
Frequently Asked Questions
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.
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.
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.
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.