How to Test a Regex Online Before You Ship It
A regular expression is quick to write and surprisingly easy to get subtly wrong. It matches too much, too little, or falls over on an input you never thought to try. The pattern that validated your three test strings in five seconds can still reject a real customer's email address or swallow half a log file.
The fix is not to become a regex expert. It is to test the pattern against a deliberate spread of inputs before it goes anywhere that matters, and to see the results highlighted rather than infer them from the pattern text. This guide covers how to do that, the handful of mistakes behind most regex bugs, and what the MergeDoc tester does and does not show you.
Why regex bugs slip through
Reading a regex is a poor way to predict what it does. Behavior on edge cases (empty strings, trailing whitespace, accented characters, several matches on one line) is rarely obvious from the pattern itself. And most people test with the one example that prompted them to write the pattern in the first place, which is the input most likely to work.
Take the sample pattern that ships in the tester, \w+@\w+\.\w+. It matches hello@example.com perfectly. Feed it first.last@example.co.uk and it highlights only last@example.co, because \w does not match dots and the pattern allows exactly one dot after the @ sign. The pattern is not wrong for the example it was written for. It is wrong for real data, and you only find that out by trying real data.
How to test a regex step by step
- Open the Regex Tester and enter your pattern in the pattern box. Leave the flags field at
gunless you know you need others. - Paste a block of sample text into the text area. Use real data if you can: a few lines of the actual log, CSV or form input the pattern will see.
- Check the highlighted preview. Every match is highlighted in place, and a counter underneath tells you how many matches were found.
- Add inputs designed to fail. Put in the near-miss, the empty value, the one with extra spaces. Confirm the pattern ignores them.
- If the pattern is invalid, the tester shows the engine's error message in red. Read it; it usually names the unclosed group or bad escape.
- Adjust and repeat until every typical case matches, every deliberate non-case does not, and the match count is what you expect.
What to throw at your pattern
Before using a regex for form validation or data cleaning, run it against at least these:
- The typical case you designed for
- An empty or minimal input
- The same input with extra whitespace, punctuation or line breaks around it
- Two or more matches on a single line, to check that matches do not merge
- A value with unusual characters: accents, apostrophes, plus signs, a trailing dot
- A near-miss that must be rejected, such as
user@@example.comor a phone number one digit short
Keep that list next to the pattern in a comment or test file. When someone changes the regex in six months, the list tells them what it was supposed to do.
The greedy versus lazy trap
+ and * are greedy: they match as much as they possibly can. Given the text "red" and "blue", the pattern ".+" matches the whole thing from the first quote to the last, not the two words separately. Adding ? makes the quantifier lazy (".+?"), so it stops at the earliest closing quote.
Lazy quantifiers fix the symptom. The sturdier fix is to say what you actually mean: "[^"]+" matches a quote, then any run of non-quote characters, then a quote. It cannot overshoot, and it runs faster because the engine never has to backtrack.
Flags change everything
The flags field is easy to ignore and responsible for a lot of confusion:
- g finds all matches rather than the first only. The tester always scans for every match so it can highlight them all, even if you remove this flag.
- i ignores case, so
errormatchesERROR. - m makes
^and$anchor at each line instead of the whole string. Without it, a pattern like^\d+finds only a number at the very start of the text. - s lets the dot match newlines, which it does not do by default.
- u enables full Unicode matching, which matters for emoji and characters outside the basic plane.
If a pattern works on one line of sample text but fails on a pasted multi-line block, check m first.
Where typical regex testing falls short
Many online regex testers are fine, but a few habits are worth watching for. Some run your text through a server, which is a poor idea when the sample contains customer emails, tokens or log lines with internal hostnames. Some are cluttered with ads that shift the layout while you type. Others support a different regex flavor than the one you will run in production, so a pattern that passes in the tester fails in your code.
The MergeDoc tester runs entirely in your browser using the JavaScript RegExp engine, with no upload and no sign-up. That is a plus for privacy and an honest limit for compatibility. It is a JavaScript tester. It highlights matches and counts them, but it does not break out capture groups into a separate panel, and it will not tell you how Python's re or PCRE would behave. If your production language differs, use this to sanity-check the logic and then re-test in the real runtime.
Everyday uses for a quick regex test
- Cleaning an export. Strip stray whitespace or pull order numbers out of a messy spreadsheet column before pasting it back.
- Searching logs. Check that a pattern for error codes catches
E1042andE-1042but not a timestamp that happens to contain similar digits. - Validating a form field. Confirm a postcode or product-code format accepts real examples before it blocks a customer at checkout.
- Find-and-replace in an editor. Test the search side first so a global replace does not mangle a file.
- Comparing results. After a replace, put the before and after into the Text Diff Checker to see exactly what changed.
Mistakes to avoid
Do not rely on a regex for things regexes are bad at. Validating an email address fully is famously impractical; a loose check for one @ and a dot, followed by a confirmation email, beats an elaborate pattern. Parsing nested structures such as HTML or JSON is the same story. For JSON, run it through the JSON Formatter, which validates it properly instead.
Watch for unescaped special characters. A literal dot, plus, parenthesis or question mark needs a backslash. example.com also matches exampleXcom.
Avoid nested quantifiers like (a+)+ or (.). On a non-matching input they can trigger catastrophic backtracking. The tester evaluates on the page's main thread, so a runaway pattern can freeze the tab. If that happens, close it, and rewrite the pattern with a more specific character class.
Finally, anchor patterns used for validation. Without ^ and $, \d{5} happily "validates" abc123456xyz.
One more habit pays off: change one thing at a time. If a pattern stops matching after an edit, you want to know which edit broke it. Build the pattern in small pieces, confirming each piece highlights what you expect before you add the next quantifier, group or anchor. A regex assembled in one go and debugged afterward takes much longer to fix than one grown a step at a time.
Related reading
If you work with encoded or structured text, Base64 Encoding: What It Is For and CSV vs JSON cover two formats where a regex is often the wrong tool. For another case where small checks prevent expensive mistakes, see Minified JSON Debugging. More utilities for this kind of work live in the developer tools category.
Frequently asked questions
- How do I test a regular expression online?
- Paste your pattern into a regex tester, paste sample text below it, and look at what gets highlighted. Then try inputs that should not match, because a pattern that matches everything you expect can still be matching things you did not expect.
- Why does my regex match too much text?
- Almost always a greedy quantifier. `+` and `*` grab as much as they can, so `".+"` runs from the first quote to the last quote on the line. Switch to a lazy quantifier (`.+?`) or, better, a negated character class like `[^"]+`.
- What do regex flags like g, i and m do?
- `g` finds every match instead of stopping at the first, `i` ignores case, and `m` makes `^` and `$` match at each line break instead of only the start and end of the whole string. `s` lets `.` match newlines, and `u` turns on full Unicode handling.
- Is a regex tester the same in every language?
- No. Regex syntax differs between engines. The MergeDoc tester uses your browser's JavaScript engine, so a pattern that works there will behave the same in JavaScript but may need changes for Python, PCRE, Java or Go.
- Can a regex freeze my browser?
- Yes. Nested quantifiers such as `(a+)+` can cause catastrophic backtracking on certain inputs, where matching time explodes. If the tab hangs while you type a pattern, that is a sign the pattern needs to be rewritten, not that the tool is broken.
- Does my test text get uploaded anywhere?
- No. The pattern and the sample text are processed in your browser by the JavaScript RegExp engine. Nothing is sent to a server, so it is safe to paste log lines or data you would not want to share.
Tools mentioned
Compare alternatives
More in Developer Tools
Related posts
SHA-256 Hash Explained: What It Is and When to Use It
What a SHA-256 hash is, why one changed letter changes everything, how it differs from encryption, and how to generate SHA hashes free in your browser.
Unix Timestamps Explained: Epoch Time to Date and Back
What a Unix timestamp is, why it starts in 1970, seconds versus milliseconds, time zones, and the 2038 problem. Convert epoch time free in your browser.
CSS Gradients, Shadows and Border Radius, Explained
Learn the syntax behind linear-gradient, box-shadow and border-radius, with the values to start from and free generators to preview and copy the CSS.