| Pattern | Description | Example |
|---|
. | Any single character (except newline) | a.c -> abc, aXc |
^ | Start of line | ^Hello |
$ | End of line | World$ |
\ | Escape character | \. -> literal period |
| | OR (either) | cat|dog |
Character Classes
| Pattern | Description | Example |
|---|
[abc] | Any of a, b, or c | [aeiou] -> vowels |
[^abc] | Not a, b, or c | [^0-9] -> non-digit |
[a-z] | Range a to z | [A-Za-z] -> letters |
[0-9] | Range 0 to 9 | [0-9]+ -> digits |
Special Character Classes
| Pattern | Description | Equivalent |
|---|
\d | Digit | [0-9] |
\D | Non-digit | [^0-9] |
\w | Word character | [A-Za-z0-9_] |
\W | Non-word character | [^A-Za-z0-9_] |
\s | Whitespace | [ \t\n\r\f] |
\S | Non-whitespace | [^ \t\n\r\f] |
\b | Word boundary | \bword\b |
\B | Non-word boundary | |
Quantifiers
| Pattern | Description | Example |
|---|
* | 0 or more | a* -> "", a, aa, aaa |
+ | 1 or more | a+ -> a, aa, aaa |
? | 0 or 1 | colou?r -> color, colour |
{n} | Exactly n times | a{3} -> aaa |
{n,} | n or more times | a{2,} -> aa, aaa, aaaa |
{n,m} | n to m times | a{2,4} -> aa, aaa, aaaa |
Lazy (Non-Greedy) Matching
| Pattern | Description |
|---|
*? | 0 or more (lazy) |
+? | 1 or more (lazy) |
?? | 0 or 1 (lazy) |
{n,m}? | n to m times (lazy) |
"<div>a</div><div>b</div>".match(/<div>.*<\/div>/);
"<div>a</div><div>b</div>".match(/<div>.*?<\/div>/);
Grouping
| Pattern | Description | Example |
|---|
(abc) | Capture group | (\d+)-(\d+) |
(?:abc) | Non-capturing group | (?:https?|ftp):// |
(?<name>abc) | Named group | (?<year>\d{4}) |
\1, \2 | Backreference | (\w+)\s\1 -> word word |
const match = "2025-01-10".match(/(\d{4})-(\d{2})-(\d{2})/);
const match = "2025-01-10".match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
Lookahead and Lookbehind
| Pattern | Description | Example |
|---|
(?=abc) | Positive lookahead | \d+(?=px) -> “100”px |
(?!abc) | Negative lookahead | \d+(?!px) -> 100em |
(?<=abc) | Positive lookbehind | (?<=\$)\d+ -> $“100” |
(?<!abc) | Negative lookbehind | (?<!\$)\d+ |
"100px 200em".match(/\d+(?=px)/g);
"$100 200px".match(/(?<=\$)\d+/g);
Flags
| Flag | Description |
|---|
g | Global (match all occurrences) |
i | Case-insensitive |
m | Multiline mode (^ and $ match each line) |
s | Dotall (. matches newlines) |
u | Unicode support |
/pattern/gi
"Hello WORLD".match(/hello/i);
"a1b2c3".match(/\d/g);
Common Patterns
Email Address
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$
URL
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
Phone Number (Japan)
0\d{1,4}-?\d{1,4}-?\d{4}
Postal Code (Japan)
^\d{3}-?\d{4}$
Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
IP Address
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Password (8+ chars, uppercase, lowercase, and digits)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$
<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)
Full-Width Characters
[^\x01-\x7E]
Japanese Characters (Hiragana, Katakana, Kanji)
[\u3040-\u309F\u30A0-\u30FF\u4E00-\u9FAF]
Usage in JavaScript
/\d+/.test("abc123");
"hello123world456".match(/\d+/g);
"hello world".replace(/world/, "JavaScript");
"a1b2c3".replace(/\d/g, "X");
"a,b;c".split(/[,;]/);
"hello world".search(/world/);
Usage in Python
import re
re.match(r"\d+", "123abc")
re.search(r"\d+", "abc123")
re.findall(r"\d+", "a1b2c3")
re.sub(r"\d+", "X", "a1b2c3")
re.split(r"[,;]", "a,b;c")
pattern = re.compile(r"\d+")
pattern.findall("a1b2c3")
Related Articles
← Back to list