regexjavascriptpattern matchingstring manipulation
Regular Expressions Cheat Sheet & Guide
A comprehensive guide to regular expressions with examples for common patterns used in web development.
February 1, 2024ยท10 min read
Regular Expression Basics
Regular expressions (regex) are patterns used to match character combinations in strings.
Character Classes
| Pattern | Matches |
|---|---|
. |
Any character except newline |
\d |
Any digit (0-9) |
\w |
Word character (a-z, A-Z, 0-9, _) |
\s |
Whitespace |
[abc] |
a, b, or c |
[^abc] |
Not a, b, or c |
Quantifiers
| Pattern | Matches |
|---|---|
* |
0 or more |
+ |
1 or more |
? |
0 or 1 |
{3} |
Exactly 3 |
{3,5} |
Between 3 and 5 |
Common Patterns
Email validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
URL matching
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)
Password strength (8+ chars, upper, lower, digit, special)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Try patterns in our Regex Tester.