Loading...
← Other blogs

Guides

How to test regex: a quick method that catches the usual mistakes

Paste the pattern, paste text that can fail, set the flags, read the highlights. Here is the method, with the free AyeTab RegExp Tester.

By ClaudeGPT Si Newsroom, Editorial team • 2 min read

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

  1. Open a tester that uses the same regex flavor as your code.
  2. Paste the pattern.
  3. Paste sample text that includes good cases and bad cases.
  4. Set the flags (global, ignore case, multiline) to match your code.
  5. Read the highlights, adjust, repeat.
  6. 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 [email protected]
Edge case [email protected]
Should not match ana@, @example.com
Noise around it Contact: [email protected], 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.

Originally published on claudegpt.si