Test every pattern in this guide using the free regex tester — paste the pattern, enter test strings, and see matches highlighted instantly.
Character classes
| Pattern | Matches | Example |
|---|---|---|
| \d | Any digit (0–9) | \d+ matches "42" in "abc42" |
| \D | Any non-digit | \D+ matches "abc" in "abc42" |
| \w | Word char: [a-zA-Z0-9_] | \w+ matches "hello_world" |
| \W | Non-word character | \W matches spaces, punctuation |
| \s | Whitespace (space, tab, newline) | \s+ collapses multiple spaces |
| . | Any character except newline | c.t matches "cat", "cut", "c4t" |
| [abc] | a, b, or c | [aeiou] matches vowels |
| [^abc] | NOT a, b, or c | [^0-9] matches non-digits |
| [a-z] | Lowercase a through z | [a-zA-Z] matches any letter |
Quantifiers
| Quantifier | Meaning | Example |
|---|---|---|
| * | 0 or more | ab* matches "a", "ab", "abbb" |
| + | 1 or more | \d+ matches one or more digits |
| ? | 0 or 1 (optional) | colou?r matches "color" and "colour" |
| {n} | Exactly n times | \d{4} matches exactly 4 digits |
| {n,} | n or more times | \d{3,} matches 3+ digits |
| {n,m} | Between n and m times | \d{2,4} matches 2, 3, or 4 digits |
Anchors and boundaries
| Pattern | Meaning |
|---|---|
| ^ | Start of string (or line in multiline mode) |
| $ | End of string (or line in multiline mode) |
| \b | Word boundary — between \w and \W |
| \B | Non-word boundary |
^\d+$ matches a string that contains only digits (nothing else). Without anchors, \d+ would match the digits inside any string, including strings with other characters.
Groups and alternation
(cat|dog) # Matches "cat" or "dog"
(https?) # Matches "http" or "https" (s is optional)
(?:abc) # Non-capturing group — groups without creating a backreference
(\w+)@(\w+) # Capture groups — \1 = username, \2 = domainNon-capturing groups (?:...)are useful when you need to group for alternation but don't need the matched text — they're slightly faster and don't pollute your backreference list.
The patterns you'll use most
# Email (basic — full RFC 5322 compliance is impractical)
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
# URL
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b[-a-zA-Z0-9@:%_+.~#?&/=]*
# IPv4 address
^(\d{1,3}\.){3}\d{1,3}$
# Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
# US phone number
^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
# Hex color
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
# Slug (URL-safe string)
^[a-z0-9]+(?:-[a-z0-9]+)*$
# Whitespace trimmer (replace with empty string)
^\s+|\s+$
# HTML tag stripper (remove all tags)
<[^>]*>Flags that change matching behavior
i(case-insensitive):/hello/imatches "Hello", "HELLO", "hello"g(global): Find all matches, not just the first onem(multiline):^and$match start/end of each line, not just the whole strings(dotAll):.matches newlines too
Common mistakes
- Forgetting to escape dots.
.in regex means "any character." To match a literal dot, use\.. The regexthefreeaitools.comalso matches "thefreeaitools_com" — usethefreeaitools\.com. - Greedy vs. lazy matching.
<.+>is greedy — it matches from the first<to the LAST>. Use<.+?>for lazy matching (shortest possible match). - Catastrophic backtracking. Nested quantifiers like
(a+)+on a string that doesn't match can cause exponential backtracking and hang the browser. Avoid patterns with nested quantifiers on large inputs.
Related tools
- Free Regex Tester — test patterns against real strings with live highlighting
Written by Achraf A., founder of TheFreeAITools.