Regular expression syntax, flags, lookarounds and common patterns.
Characters
| Pattern |
Matches |
. |
Any character except newline |
\d / \D |
Digit / non-digit |
\w / \W |
Word character [A-Za-z0-9_] / non-word |
\s / \S |
Whitespace / non-whitespace |
\b / \B |
Word boundary / non-boundary |
\. |
Literal dot (escape special characters) |
Character classes
| Pattern |
Matches |
[abc] |
a, b or c |
[^abc] |
Anything except a, b or c |
[a-z] |
Lowercase letter |
[a-zA-Z0-9] |
Alphanumeric |
Anchors
| Pattern |
Matches |
^ |
Start of string (or line with m) |
$ |
End of string (or line with m) |
Quantifiers
| Pattern |
Matches |
* |
0 or more |
+ |
1 or more |
? |
0 or 1 |
{3} |
Exactly 3 |
{2,} |
2 or more |
{2,5} |
2 to 5 |
*? / +? |
Lazy (as few as possible) |
Groups
| Pattern |
Meaning |
(abc) |
Capture group |
(?:abc) |
Non-capturing group |
(?<name>abc) |
Named group |
\1 |
Backreference to group 1 |
a|b |
a or b |
Lookarounds
| Pattern |
Meaning |
x(?=y) |
x followed by y |
x(?!y) |
x not followed by y |
(?<=y)x |
x preceded by y |
(?<!y)x |
x not preceded by y |
Flags
| Flag |
Meaning |
g |
Global, find all matches |
i |
Case insensitive |
m |
^ and $ match per line |
s |
. also matches newline |
u |
Unicode |
Common patterns
| Use |
Pattern |
| Email (simple) |
^[^\s@]+@[^\s@]+\.[^\s@]+$ |
| URL |
https?:\/\/[^\s/$.?#].[^\s]* |
| ISO date |
^\d{4}-\d{2}-\d{2}$ |
| Time (24h) |
^([01]\d|2[0-3]):[0-5]\d$ |
| Hex color |
^#?([0-9a-fA-F]{3}){1,2}$ |
| Whole word |
\bword\b |
| Trim whitespace |
^\s+|\s+$ |
| Slug |
^[a-z0-9]+(?:-[a-z0-9]+)*$ |
JavaScript usage
const pattern = /(?<year>\d{4})-(?<month>\d{2})/;
const match = '2026-10'.match(pattern);
match.groups.year; // '2026'
/^\d+$/.test('123'); // true
'a-b-c'.replaceAll(/-/g, '_'); // 'a_b_c'
[...'a1b2'.matchAll(/\d/g)].map((m) => m[0]); // ['1', '2']