DEV Community

Anup Karanjkar
Anup Karanjkar

Posted on • Originally published at wowhow.cloud

Regex Tester & Cheat Sheet: Master Regular Expressions in 2026

Regular expressions are the most transferable skill in programming. The same pattern syntax works in JavaScript, Python, Go, Java, Ruby, PHP, bash, sed, grep, and dozens of other languages and tools. Learning regex once gives you a superpower that works everywhere.

This guide covers the complete regex syntax with examples, common patterns you can use immediately, and how to use our live regex tester to build and debug your own patterns. Bookmark this page — it is the reference I keep open every day.

Try it yourself: Free Regex Tester — free, no signup, runs in your browser.

Regex Syntax Quick Reference

Anchors

  • ^ — Start of string (or start of line in multiline mode)

  • $ — End of string (or end of line in multiline mode)

  • — Word boundary (between a word character and a non-word character)

  • B — Not a word boundary

  • A — Start of string (Python; not supported in JS)

  • Z — End of string (Python; not supported in JS)

Character Classes

  • . — Any character except newline (use [sS] to include newlines)

  • d — Digit (0-9)

  • D — Not a digit

  • w — Word character (a-z, A-Z, 0-9, underscore)

  • W — Not a word character

  • s — Whitespace (space, tab, newline, carriage return)

  • S — Not whitespace

  • [abc] — Any one of a, b, or c

  • [^abc] — Any character except a, b, or c

  • [a-z] — Any character in range a through z

  • [a-zA-Z0-9] — Alphanumeric

Quantifiers

  • * — Zero or more (greedy)

  • + — One or more (greedy)

  • ? — Zero or one (greedy)

  • {n} — Exactly n times

  • {n,} — n or more times

  • {n,m} — Between n and m times (inclusive)

  • *? — Zero or more (lazy — matches as few as possible)

  • +? — One or more (lazy)

  • ?? — Zero or one (lazy)

Greedy vs. lazy matters. The pattern <.+> applied to <div>Hello</div> matches the entire string (greedy, matches as much as possible). The pattern <.+?> matches only <div> (lazy, matches as little as possible). For HTML parsing, lazy quantifiers almost always give the intended result.

Groups and References

  • (abc) — Capturing group

  • (?:abc) — Non-capturing group (groups without creating a backreference)

  • (?<name>abc) — Named capturing group

  • \1 — Backreference to group 1

  • \k<name> — Backreference to named group

Lookaheads and Lookbehinds

  • (?=abc) — Positive lookahead (followed by abc)

  • (?!abc) — Negative lookahead (not followed by abc)

  • (?<=abc) — Positive lookbehind (preceded by abc)

  • (?<!abc) — Negative lookbehind (not preceded by abc)

Lookaheads and lookbehinds are zero-width assertions — they match a position but do not consume characters. They are essential for patterns like "find all prices" ((?<=$)d+.d{2}) or "find all words not followed by a comma" (w+(?!,)).

Flags

  • g — Global (find all matches, not just the first)

  • i — Case insensitive

  • m — Multiline (^ and $ match start/end of each line)

  • s — Dotall (. matches newlines)

  • u — Unicode mode (treat pattern as Unicode code points)

  • x — Extended/verbose mode (allows whitespace and comments in pattern) — Python, not JS

Common Regex Patterns (Copy-Paste Ready)

Validation Patterns

Email address (reasonable approximation):

/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/
Enter fullscreen mode Exit fullscreen mode

Indian mobile number:

/^(+91|91|0)?[6-9]d{9}$/
Enter fullscreen mode Exit fullscreen mode

Indian GSTIN:

/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/
Enter fullscreen mode Exit fullscreen mode

PAN card number:

/^[A-Z]{5}[0-9]{4}[A-Z]{1}$/
Enter fullscreen mode Exit fullscreen mode

URL (including http/https):

/https?://(www.)?[-a-zA-Z0-9@:%._+~#=]{1,256}.[a-zA-Z0-9()]{1,6}([-a-zA-Z0-9()@:%_+.~#?&=]*)/
Enter fullscreen mode Exit fullscreen mode

IPv4 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]?)$/
Enter fullscreen mode Exit fullscreen mode

Strong password (min 8 chars, 1 uppercase, 1 lowercase, 1 digit, 1 special):

/^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[@$!%*?&])[A-Za-zd@$!%*?&]{8,}$/
Enter fullscreen mode Exit fullscreen mode

Extraction Patterns

Extract all numbers from a string:

/d+.?d*/g
Enter fullscreen mode Exit fullscreen mode

Extract content between HTML tags:

/([sS]*?)/g
Enter fullscreen mode Exit fullscreen mode

Extract all hashtags:

/#[w]+/g
Enter fullscreen mode Exit fullscreen mode

Extract all mentions (@username):

/@[w]+/g
Enter fullscreen mode Exit fullscreen mode

Extract key-value pairs from query string:

/([^&=?]+)=([^&]*)/g
Enter fullscreen mode Exit fullscreen mode

Transformation Patterns

Convert camelCase to kebab-case:

/([a-z])([A-Z])/g   replace with $1-$2, then toLowerCase()
Enter fullscreen mode Exit fullscreen mode

Convert snake_case to camelCase:

/_([a-z])/g   replace with match[1].toUpperCase()
Enter fullscreen mode Exit fullscreen mode

Remove extra whitespace (collapse multiple spaces to one):

/s{2,}/g  → replace with ' '
Enter fullscreen mode Exit fullscreen mode

Remove HTML tags:

/]*>/g  → replace with ''
Enter fullscreen mode Exit fullscreen mode

How to Use the Live Regex Tester

Our regex tester provides a real-time environment for building and debugging patterns:

  1. Enter your pattern in the pattern field, without the wrapping slashes

  2. Select your flags using the flag toggles (g, i, m, s are the most common)

  3. Paste your test string in the text area

  4. View matches highlighted in real time as you type

  5. See capture groups listed separately — named groups show their names

  6. Get the match count and any replacement output if you enter a replacement string

The tester also shows performance warnings for catastrophic backtracking patterns — patterns that can cause exponential time complexity on certain inputs, a common source of ReDoS (Regular Expression Denial of Service) vulnerabilities.

Debugging Catastrophic Backtracking

Catastrophic backtracking occurs when a regex engine explores exponentially many possible match paths before determining there is no match. The classic example: (a+)+ applied to a long string of a's followed by a non-matching character. The engine tries every possible way to partition the a's between the inner and outer group before failing.

Warning signs: patterns with nested quantifiers on overlapping character classes ((a*)*, (w+s?)*), or alternation with common prefixes without proper anchoring.

Fix: use possessive quantifiers if your regex engine supports them, atomic groups, or restructure the pattern to avoid ambiguity in the match paths.

Regex in Different Languages: Key Differences

JavaScript: Supports lookaheads and lookbehinds (ES2018+). No A/Z. Unicode mode with u flag. Named groups with (?<name>).

Python: Use the re module. Supports verbose mode (re.VERBOSE), A/Z. Named groups with (?P<name>). The regex third-party module adds possessive quantifiers and atomic groups.

Go: Uses RE2 syntax. No lookaheads or lookbehinds. No backreferences. Fast and safe for untrusted input.

Java: Supports lookaheads, lookbehinds, possessive quantifiers, and atomic groups. Unicode support via Pattern.UNICODE_CHARACTER_CLASS.

People Also Ask

How do I test a regex pattern online?

Use our free regex tester — paste your pattern and test string, select your flags, and see matches highlighted in real time. It shows capture groups, match count, and replacement output.

What is the difference between greedy and lazy quantifiers?

Greedy quantifiers (*, +, ?) match as much as possible. Lazy quantifiers (*?, +?, ??) match as little as possible. For most text extraction tasks, lazy quantifiers give the intended result when matching delimited content.

How do I match a literal dot in regex?

Escape it with a backslash: .. An unescaped dot matches any character. To match a literal dot in an email pattern, use . rather than ..

What is a lookahead in regex?

A lookahead ((?=...)) matches a position only if followed by the specified pattern, without consuming the matched characters. It is a zero-width assertion useful for conditional matching, such as finding all numbers followed by a percent sign.

For more developer tools, visit the WOWHOW tools catalog — including tools for JSON formatting, color contrast checking, and API cost estimation.

Originally published at wowhow.cloud

Top comments (0)