How to test a regular expression
Short answer
Paste your pattern and some sample text into a regex tester and watch the matches highlight live. Adjust the pattern until only the intended text matches, then copy it into your code with the right flags.
Why test before you ship
A regex that looks right often matches too much or too little on real input. Testing against representative samples — including edge cases — catches this before it reaches production. For the syntax itself, see regex explained.
Test a pattern
- Paste sample textInclude the strings that should match and some that should not.
- Enter the patternMatches highlight live as you type, so you see the effect.
- Set flagsAdd g for all matches, i for case-insensitive, m for multiline.
- Refine and copyTighten the pattern until only the intended text matches.
Common debugging fixes
- Escape special characters you mean literally:
.?(become\.\?\( - Use anchors
^and$to match the whole string, not just part - Prefer lazy quantifiers
*?when greedy*grabs too much - Remember to add the
gflag if you expect multiple matches