Regex Tester Cheatsheet for Beginners and Developers
Regular expressions are one of the most powerful—and most feared—tools in a developer's arsenal. This practical guide covers the core syntax, real-world patterns, and common pitfalls so you can write and test regex confidently.
Reserved ad container for responsive Google AdSense display units
What Is a Regular Expression?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. The pattern can match strings, extract substrings, validate input format, or perform find-and-replace operations. Regex engines are built into virtually every programming language and text editor.
The syntax can look cryptic—^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$ is a simplified email validator—but it becomes readable once you understand the building blocks. This guide breaks them all down.
Regex Fundamentals: Literal Characters and Metacharacters
Most characters in a regex match themselves literally. The letter a matches the character "a". But some characters have special meaning and are called metacharacters:
. ^ $ * + ? { } [ ] | ( )
To match a literal metacharacter, escape it with a backslash: . matches a literal dot; $ matches a dollar sign.
Character Classes
Character classes match one character from a defined set:
[abc]— matches a, b, or c[a-z]— matches any lowercase letter[A-Z]— matches any uppercase letter[0-9]— matches any digit (equivalent tod)[^abc]— matches any character EXCEPT a, b, or c (negated class)[a-zA-Z0-9_]— matches any word character (equivalent tow)
Shorthand Character Classes
d— digit (0–9)D— non-digitw— word character (a–z, A–Z, 0–9, _)W— non-word characters— whitespace (space, tab, newline, etc.)S— non-whitespace.— any character except newline (use[sS]to include newlines)
Anchors
Anchors don't match characters—they match positions:
^— start of string (or start of line in multiline mode)$— end of string (or end of line in multiline mode)— word boundary (betweenwandW)B— non-word boundary
Example: ^d{5}$ matches exactly a 5-digit US zip code and nothing else—the anchors prevent matching "12345extra".
Quantifiers
Quantifiers specify how many times the preceding element must appear:
*— zero or more times+— one or more times?— zero or one time (makes the element optional){n}— exactly n times{n,}— n or more times{n,m}— between n and m times (inclusive)
By default, quantifiers are greedy—they match as much as possible. Add ? after a quantifier to make it lazy (matches as little as possible): .*? vs .*.
Capturing Groups and Backreferences
Parentheses create a capturing group, saving the matched substring for later use:
(d{4})-(d{2})-(d{2})
This matches a date like "2026-09-20" and captures the year in group 1, month in group 2, day in group 3. In replacement strings, reference them as $1, $2, $3 (or \\1, \\2 in some flavors).
Use (?:...) for a non-capturing group when you need grouping for quantifiers or alternation but don't need the captured value.
Lookaheads and Lookbehinds
Lookaheads and lookbehinds are zero-width assertions—they check what's before or after the current position without including it in the match:
(?=...)— positive lookahead: position must be followed by the pattern(?!...)— negative lookahead: position must NOT be followed by the pattern(?<=...)— positive lookbehind: position must be preceded by the pattern(?<!...)— negative lookbehind: position must NOT be preceded by the pattern
Example: d+(?= dollars) matches a number only if it's followed by " dollars"—the word "dollars" is not part of the match result.
Regex Flags
i— case-insensitive matchingg— global (find all matches, not just the first)m— multiline (^ and $ match start/end of each line)s— dotAll (. matches newline characters too)u— Unicode mode (correct handling of Unicode code points)
Practical Regex Patterns
- Email:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$ - URL:
https?://[^s/$.?#].[^s]* - US phone (flexible):
[+]?[(]?[0-9]{3}[)]?[-s.]?[0-9]{3}[-s.]?[0-9]{4} - IPv4 address:
(?:d{1,3}.){3}d{1,3} - Hex color:
#(?:[0-9a-fA-F]{3}){1,2} - Date (YYYY-MM-DD):
d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]d|3[01]) - Strong password:
^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[@$!%*?&])[A-Za-zd@$!%*?&]{8,}$
Common Regex Mistakes
- Catastrophic backtracking: Patterns like
(a+)+on long non-matching strings can cause exponential runtime. Use possessive quantifiers or atomic groups where supported. - Forgetting to anchor validators:
d+matches "123" in "abc123def". Use^d+$to validate the entire string. - Assuming a regex for all email validation: RFC 5322 email addresses are complex enough that a regex cannot correctly validate all cases. Use a dedicated library for strict validation.
- Not escaping the dot in domains:
.commatches "Xcom". Use.com.
Write, test, and debug regex patterns in real time. Open ZapyNext's free Regex Tester—paste your pattern and test string, see matches highlighted instantly, switch between JavaScript and PCRE modes, and use the built-in reference panel while you work.
Reserved ad container for responsive Google AdSense display units