Regex Testing Guide: Syntax, Common Patterns & Cheatsheet

Updated March 2026 · 8 min read

Regular expressions (regex) are one of the most powerful tools in a developer's toolkit. They let you search, match, validate, and transform text with precision. While regex syntax can feel intimidating at first, mastering the basics opens up efficient solutions for data validation, log parsing, text extraction, and more.

Regex Syntax Quick Reference

PatternDescriptionExample
.Any character (except newline)h.t matches "hat", "hot"
\dAny digit (0-9)\d{3} matches "123"
\wWord character (a-z, A-Z, 0-9, _)\w+ matches "hello_2"
\sWhitespace (space, tab, newline)\s+ matches spaces
^Start of string/line^Hello matches at start
$End of string/lineend$ matches at end
*0 or more repetitionsab*c matches "ac", "abc"
+1 or more repetitionsab+c matches "abc", "abbc"
?0 or 1 (optional)colou?r matches both spellings
{n,m}Between n and m repetitions\d{2,4} matches 2-4 digits
[abc]Character class (a, b, or c)[aeiou] matches vowels
[^abc]Negated class (not a, b, c)[^0-9] matches non-digits
(group)Capture group(\d+)-(\d+) captures both
a|bAlternation (a or b)cat|dog matches either
\bWord boundary\bword\b whole word only

Regex Flags Explained

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitive/hello/i matches "Hello", "HELLO"
mMultiline^ and $ match line starts/ends
sDotall. matches newlines too
uUnicodeEnables full Unicode matching

Common Regex Patterns for Validation

Email Address

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

// Matches: [email protected], [email protected]
// Fails:   @example.com, user@, [email protected]

Note: email validation via regex is inherently imperfect. For production systems, send a verification email rather than relying solely on regex.

URL

^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$

// Matches: https://devtoolkit.ink/tools/regex
// Matches: http://example.com?q=test&page=1

Phone Number (International)

^\+?[1-9]\d{1,14}$

// E.164 format
// Matches: +14155552671, +905551234567
// Fails:   0555-123-4567 (local format)

IPv4 Address

^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$

// Matches: 192.168.1.1, 10.0.0.255
// Fails:   256.1.1.1, 192.168.1

For IP network calculations, check out the Subnet Calculator.

IPv6 Address (Simplified)

^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$

// Matches: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

// Matches: 2026-03-29, 2024-12-01
// Fails:   2024-13-01, 2024-00-15

Strong Password

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

// Requires: lowercase, uppercase, digit, special char, min 8 chars

For generating secure passwords that match these requirements, use the Password Generator.

How to Use the Regex Tester

  1. Open the Regex Tester tool.
  2. Enter your regex pattern in the pattern field.
  3. Select the appropriate flags (g, i, m, s).
  4. Paste your test string in the text area.
  5. Matches are highlighted in real time as you type.
  6. Capture groups are displayed with their matched content.

Practical Regex Examples

Extract All URLs from Text

Pattern: https?://[^\s]+
Flags:   g

Input: Visit https://devtoolkit.ink and http://example.com for tools.
Matches: ["https://devtoolkit.ink", "http://example.com"]

Parse Log Timestamps

Pattern: (\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})
Flags:   g

Input: 2026-03-29 14:30:00 [INFO] Server started
Group 1: "2026-03-29"
Group 2: "14:30:00"

Find Duplicate Words

Pattern: \b(\w+)\s+\1\b
Flags:   gi

Input: The the quick brown fox fox jumped
Matches: "The the", "fox fox"

Performance Tips

FAQ

Why does my regex work in JavaScript but not Python?

Regex flavors differ between languages. JavaScript uses /pattern/flags syntax, while Python uses raw strings r"pattern". Some features like lookbehinds have varying support. Always test in the target language.

How do I match a literal dot or bracket?

Escape special characters with a backslash: \. for a literal dot, \[ for a literal bracket, \\ for a literal backslash.

What is the difference between greedy and lazy matching?

.* is greedy (matches as much as possible). .*? is lazy (matches as little as possible). For example, on <b>bold</b>, the pattern <.*> matches the entire string, while <.*?> matches only <b>.

Try Regex Tester Now →

DevToolKit — 42+ Free Online Developer Tools