My Regex Works in the Tester but Not in My Code — Here Is Why

The pattern is almost never the problem. It is escaping, flags, or a different regex engine — and each one has a distinctive symptom that tells you which of the three you are dealing with.

You build a pattern in an online tester, it matches perfectly, you paste it into your code and it matches nothing. The regex did not change, so something around it did. In practice it is one of three things: the pattern was mangled by string escaping, the flags did not travel with it, or your language uses a different regex engine than the tester.

Escaping: the pattern in your code is not the pattern you wrote

This is by far the most common cause, and it happens whenever the regex lives inside a string rather than a native regex literal. In a string, the backslash is itself an escape character, so it gets consumed before the regex engine ever sees it. Your \d becomes a plain d, and \. becomes a literal dot that now matches any character. The pattern the engine receives is genuinely different from the one you typed. The fixes differ by language: in JavaScript prefer the literal form /\d+/ over new RegExp("\\d+"); in Python use a raw string, r"\d+"; in Java and C# you have to double every backslash, or use C#'s verbatim @"..." strings.

Flags are configured separately and do not get copied

Online testers put the flags in their own little box next to the pattern, so when you select and copy, you take the pattern and leave the flags behind. Three matter most. The m flag changes ^ and $ from meaning "start and end of the whole string" to "start and end of each line" — without it, a pattern that worked against a multi-line block of test text will fail. The s flag lets . match newlines, which it does not do by default. And the i flag for case-insensitivity is the one people most often forget was switched on.

The g flag has its own trap

In JavaScript, a regex with the g flag is stateful: it keeps a lastIndex property that advances after each match. If you reuse that same regex object with .test() in a loop, you get alternating true and false results for identical input, which looks like nondeterminism and is maddening to debug. Either create the regex fresh each time, drop the g flag when you only need a boolean, or reset lastIndex to zero between calls.

Not every engine speaks the same dialect

Most online testers default to PCRE, the Perl-compatible flavour. Your code may not use it. Lookbehind assertions, named groups and possessive quantifiers all vary in support. The starkest example is RE2, used by Go and by several databases, which deliberately omits backreferences and lookahead entirely so that matching time stays linear — a pattern relying on those will not merely fail, it will refuse to compile. If your tester has an engine selector, set it to your actual language before you start building the pattern, not after it breaks.

Partial match versus full match

Testers almost always show you every partial match highlighted in the text, but many language APIs ask a different question. Java's matches() requires the pattern to consume the entire input, while find() looks for a match anywhere — so a pattern that highlights happily in a tester returns false from matches(). Python has the same three-way split between re.match, re.search and re.fullmatch. If your pattern seems correct but returns false, check which of those you called before touching the regex at all.

The one debugging step that finds it fastest

Print the compiled pattern at runtime — not the source line, the actual value the engine received. In JavaScript that is String(myRegex), in Python print(pattern.pattern). If what you see differs from what you wrote, it is an escaping problem and you have found it in one step. If it matches exactly, the problem is flags or dialect, and you have eliminated two thirds of the possibilities without guessing.

Our regex tester shows the pattern, flags and matches side by side and highlights groups as you type, so you can confirm the exact pattern and flag combination before it goes into your code. It runs in your browser, so you can safely test against real data.

→ Regex Tester