Back to Blog
Developer Guides5 min readMay 12, 2026The Toolbox Team

How to Test a Regular Expression (Regex)

Learn to test a regular expression in your browser: paste a pattern and sample text, set flags, read live matches and capture groups, and debug.

What testing a regex actually solves

A regular expression is a tiny pattern language for finding and pulling text out of strings — email addresses, phone numbers, dates, log lines, you name it. The catch is that regex is famously easy to get almost right. A pattern that looks correct can quietly match too much, too little, or nothing at all, and you won't notice until it's running in production against real data.

Testing a regex means feeding it sample text before you ship it, and watching exactly what it matches and what it captures. That's the whole point of the regex tester: you type a pattern, paste in some example text, and it highlights every match live as you type. You'd want this whenever you're writing a validation rule, a search-and-replace, a scraper, or a log parser — basically any time the cost of a wrong pattern is higher than the two minutes it takes to check it. The tester runs entirely in your browser, so the text you paste in (including anything sensitive from logs or exports) never leaves your device. No upload, no account.

How to test a regular expression

  1. Open the regex tester. It loads as a single page with a pattern field, a flags toggle, and a large text area for your sample input. Nothing to install.

  2. Type your pattern into the pattern field. Enter it without the slashes — for example \d{3}-\d{4} to match a phone fragment, or [\w.-]+@[\w.-]+\.\w+ for a rough email. As you type, the tool parses the pattern and flags syntax errors (an unclosed group, a stray bracket) immediately, so you don't have to run anything to find a typo.

  3. Set your flags. Toggle the common ones: g (global — find every match, not just the first), i (ignore case), m (multiline, so ^ and $ match at each line break), and s (dotall, so . also matches newlines). If you're testing against a multi-line block like a log file, you almost always want g on and usually m too.

  4. Paste your sample text into the test area. Use real examples — copy a few actual rows from your data, including the weird edge cases: empty values, extra whitespace, the one record that's formatted differently. Testing only against clean input is how bad patterns slip through.

  5. Read the highlighted matches. Every substring your pattern matches is highlighted right in the text, with alternating colors so adjacent matches are easy to tell apart. Scan for two failure modes: text that should match but isn't highlighted (your pattern is too strict), and text that shouldn't match but is (it's too loose).

  6. Check your capture groups. Each match is broken out into its numbered groups, named groups ((?<year>\d{4})), backreferences, and lookarounds. This is the part most people skip — confirm that group 1 actually holds the area code, group 2 the rest, and so on. If a group is empty or grabbing the wrong slice, you'll see it here instead of in your code later.

  7. Try the replace/substitution box if you're doing a find-and-replace. Enter a replacement string using $1, $2, or $<name> to reference your captures (for example, reformatting 2026-06-05 into 06/05/2026). The tool shows the rewritten output so you can confirm the substitution before pasting the regex into your editor or script.

  8. Iterate. Tweak the pattern, watch the highlights shift in real time, and stop when every match — and only the right matches — lights up. Then copy the finished pattern straight into your code.

Tips

  • Anchor when you mean "the whole thing." For validation (a field that must be entirely a valid value), wrap your pattern in ^...$. Without anchors, \d{4} happily matches inside abc1234xyz, which is fine for search but wrong for validation.
  • Prefer specific over greedy. .* matches as much as possible and is the usual cause of "it matched way more than I expected." Reach for [^,]*, \d+, or a non-greedy .*? instead.
  • Watch the flavor. This tester uses JavaScript regex. Most syntax is portable, but lookbehind, named groups, and a few escapes behave differently in Python, PCRE, or Go. If your target language differs, double-check those specific features.
  • Forgot a token? Keep the regex cheat sheet open in a second tab to look up \b, \s, lazy quantifiers, and lookaround syntax while you build.

Common problems

  • Nothing matches. Check the flags first — a pattern written for multiline input does nothing useful without m, and case-sensitivity bites constantly (turn on i). Then verify you didn't paste the slashes into the pattern field.
  • It matches too much. A greedy .* or .+ is the culprit nine times out of ten. Make it lazy (.*?) or replace . with a tighter character class.
  • Special characters misbehave. Dots, parentheses, brackets, and +/*/? are operators. To match them literally, escape them: \., \(, \$.
  • Group numbers don't line up. Every ( opens a numbered group, including ones you only added for grouping. Use a non-capturing group (?:...) when you want to group without consuming a number.

FAQ

Is my pasted text uploaded anywhere? No. The matching runs in your browser with JavaScript, so your pattern and sample text stay on your device. That makes it safe for log snippets, exports, or anything you'd rather not send to a server.

Which regex syntax does it support? JavaScript's engine, including named capture groups, backreferences, and lookahead/lookbehind. If you're targeting Python or PCRE, the common syntax matches, but verify the more advanced features against your runtime.

Why does my pattern work here but fail in my code? Usually flags or escaping. Make sure your code applies the same flags (often the global flag changes behavior), and that backslashes are doubled if your language requires escaping them inside string literals.

Can I test a replacement, not just a match? Yes — use the replace box with $1/$<name> references to preview the substituted output before committing the pattern.

Working through more text tasks? Compare two versions side by side with the diff checker, pull values out of JSON with the JSON path finder, or browse the developer tools guide for the full set.