A regular expression (regex) is a sequence of characters that defines a search pattern. Regex is used in almost every programming language for searching, validating, and transforming text. Testing regex interactively — before embedding it in code — prevents bugs that are notoriously hard to debug later.
Regex basics
| Pattern | Matches |
|---|---|
. |
Any single character |
\d |
Any digit (0–9) |
\w |
Word character (a-z, A-Z, 0-9, _) |
\s |
Whitespace (space, tab, newline) |
^ |
Start of line |
$ |
End of line |
* |
0 or more of preceding |
+ |
1 or more of preceding |
? |
0 or 1 of preceding |
{n,m} |
Between n and m of preceding |
[abc] |
Any of a, b, or c |
[^abc] |
Not a, b, or c |
(abc) |
Capture group |
a|b |
a or b |
Common regex flags
g— Global: find all matches, not just the firsti— Case insensitive:[a-z]also matches[A-Z]m— Multiline:^and$match start/end of each lines— Dotall:.matches newline characters too
Useful regex patterns
Email validation:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
URL matching:
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}
Phone number (flexible):
[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}
Date (YYYY-MM-DD):
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
IPv4 address:
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
How to test regex online
- Go to Regex Tester
- Enter your regular expression in the pattern field
- Set flags (g, i, m) as needed
- Paste or type test text in the input area
- Matches are highlighted instantly; capture groups shown below
Regex pitfalls to avoid
- Catastrophic backtracking — nested quantifiers like
(a+)+can freeze on long inputs; test with worst-case strings - Anchors — forgetting
^and$lets partial matches pass validation - Greedy vs lazy —
.*is greedy (takes as much as possible);.*?is lazy (takes as little as possible)