Key takeaways
- To test a regex, run it against sample text that includes both cases that should match and cases that should not.
- Match the flags and the regex flavor to the code the pattern will run in, and anchor with ^ and $ when validating a whole field.
- The free AyeTab RegExp Tester highlights matches as you type, flags syntax errors and keeps your text in the browser.
How to test regex, in one sentence: paste the pattern and a few lines of realistic sample text into a regex tester, set the flags your code will use, and check that it matches what it should and skips what it should not. The free AyeTab RegExp Tester is the quickest way to do it: it highlights matches as you type and keeps your text in the browser.
The short version
- Open a tester that uses the same regex flavor as your code.
- Paste the pattern.
- Paste sample text that includes good cases and bad cases.
- Set the flags (global, ignore case, multiline) to match your code.
- Read the highlights, adjust, repeat.
- Copy the final pattern into your code and add a unit test.
Step by step in AyeTab
Open the RegExp Tester on app.ayetab.com, or press Ctrl+K or Cmd+K on your new tab if you have the AyeTab extension for Chrome and type "regex".
Type the pattern between the slashes. You write the expression itself, without the surrounding slashes or quotes your code might use. Then paste the sample text into the left pane. The right pane repeats the text with every match marked, so you can see at a glance which lines hit.
Toggle the flags with the g, i and m buttons. If your code calls str.match(/.../gi), switch on g and i here as well. When you make a syntax mistake, such as an unclosed group, the tester shows an error instead of an empty result, which tells you the problem is the syntax, not the logic.
The tool uses your browser's JavaScript regex engine, so the result is exactly what JavaScript and TypeScript code will do.
Pick sample text that can fail
The most common testing mistake is to paste only the strings you expect to match. A pattern that matches everything passes that test. Build a small set that covers:
| Case | Example for an email pattern |
|---|---|
| Plain valid input | ana@example.com |
| Edge case | first.last+tag@sub.example.co |
| Should not match | ana@, @example.com |
| Noise around it | Contact: ana@example.com, thanks |
If the "should not match" lines light up, the pattern is too loose. If the edge case stays dark, it is too strict.
Check the flags and anchors
Two details cause most surprises. Without the g flag, many methods stop at the first match. With the m flag, ^ and $ match at every line start and end, not only at the start and end of the whole text. When you validate a whole field, anchor the pattern with ^ and $ and test it with m off.
Keep the text private
Sample text often comes from real logs or exports. A tester that runs locally keeps it on your machine. AyeTab processes everything in the browser, uploads nothing, needs no account and still works offline after the page has loaded.
When to use another tool
If your pattern runs in Python, Java or PCRE, test it in a tester that supports that flavor, such as regex101, which also explains each token. RegExr is useful when you want a reference and saved tests next to the pattern. For JavaScript patterns and private data, AyeTab is the direct route.
Frequently asked questions
How do I test a regex without writing code?
Use an online regex tester. In the AyeTab RegExp Tester you type the pattern, paste sample text and see each match highlighted, with no account needed.
Why does my regex match in the tester but not in my code?
Usually the flags differ, or the tester uses another regex flavor than your language. Check both, and remember that string literals in code may need extra backslashes.
Should I still write unit tests for a regex?
Yes. A tester helps you build the pattern; a unit test with the same good and bad samples keeps it from breaking later.