Regular expressions are the most compact way to search and transform text — and the easiest thing to get subtly wrong. The fix is live testing: type a pattern, watch which text matches, and adjust until it's right. This guide covers the patterns you'll actually use, the classic mistakes, and how to test regex in your browser.
The Core Patterns You'll Actually Use
| Pattern | Meaning | Example |
|---|---|---|
\d | Any digit | \d{4} = 4 digits |
\w | Word character | \w+ = a word |
^ $ | Start / end of string | ^admin = starts with admin |
[...] | Character class | [a-z0-9] = lowercase or digit |
(...) | Capture group | (\d{2})-(\d{2}) |
* + ? | Quantifiers | ab+c = a, one+ b, c |
Classic Mistakes
- Forgetting to escape:
.means "any character" — use\.for a literal dot. - Greedy matching:
.*matches as much as possible; add?(.*?) for lazy matching. - Anchoring confusion:
^/$without themflag only match the whole string, not each line. - Overly broad classes:
\walso matches underscore —[A-Za-z0-9]may be what you meant.
Flags in 30 Seconds
g— global: find all matches, not just the first.i— case-insensitive.m— multiline:^/$match line boundaries.s— dot matches newlines too.u— Unicode mode (needed for emoji-aware classes).
How to Test Regex Online
- Open the tool: go to the regex tester.
- Type your pattern: matches highlight live in the test text as you type.
- Toggle flags: switch
g/i/m/s/uand watch matches change instantly. - Inspect capture groups: each match shows its groups for extraction debugging.
- Preview replacement: type a replacement to see the transformed text before applying it in code.
Tip: start from real input, not an imagined one. Paste actual sample data and test edge cases — empty strings, punctuation, uppercase — before shipping the pattern.
Frequently Asked Questions
Why doesn't my regex match anything?
Check escaping (e.g. \. for dots), anchors, and whether the m flag is needed for line-based input. Testing live shows exactly where the pattern stops matching.
What does the g flag do?
Without g, a regex stops after the first match; with g, it finds every match in the text. The tester highlights all matches only when g is on.
Can a regex be too slow?
Yes — patterns with nested quantifiers like (a+)+ can cause catastrophic backtracking. Keep patterns simple and test against large inputs.
Is my text uploaded when I test regex?
No. The regex tester runs entirely locally in your browser.
References
- Regular expressions — MDN Web Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
- Catastrophic backtracking — regular-expressions.info: https://www.regular-expressions.info/catastrophic.html