Test every pattern in this guide using the free regex tester — paste the pattern, enter test strings, and see matches highlighted instantly.

Character classes

PatternMatchesExample
\dAny digit (0–9)\d+ matches "42" in "abc42"
\DAny non-digit\D+ matches "abc" in "abc42"
\wWord char: [a-zA-Z0-9_]\w+ matches "hello_world"
\WNon-word character\W matches spaces, punctuation
\sWhitespace (space, tab, newline)\s+ collapses multiple spaces
.Any character except newlinec.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

QuantifierMeaningExample
*0 or moreab* 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

PatternMeaning
^Start of string (or line in multiline mode)
$End of string (or line in multiline mode)
\bWord boundary — between \w and \W
\BNon-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 = domain

Non-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/i matches "Hello", "HELLO", "hello"
  • g (global): Find all matches, not just the first one
  • m (multiline): ^ and $ match start/end of each line, not just the whole string
  • s (dotAll): . matches newlines too

Common mistakes

  • Forgetting to escape dots. . in regex means "any character." To match a literal dot, use \.. The regex thefreeaitools.com also matches "thefreeaitools_com" — use thefreeaitools\.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


Written by Achraf A., founder of TheFreeAITools.