Why a regex works in regex101 but not in JavaScript
A pattern that lights up every match on regex101 throws an error in your code, or worse, runs and matches nothing. Almost always it is one of a short list of reasons, and each can be checked in a minute.
Written · Updated
regex101 opens on the PCRE flavour, the engine PHP uses, and JavaScript is a different engine. A pattern written there can use syntax JavaScript does not have (inline flags, atomic groups, possessive quantifiers, \A, Python’s (?P<name>)), which throws an error or, for \A, quietly means something else. If the flavour was already ECMAScript, the cause is usually escaping: new RegExp("\d+") loses its backslash. Set regex101 to ECMAScript, or test the pattern where it will run.
First, the flavour
The flavour list sits on the left of regex101, and a new tab starts on PCRE2. Switch it to ECMAScript (JavaScript) and the page uses the browser’s own engine: a pattern that only worked because of PCRE now shows the same error your code does. That one change settles most cases.
What JavaScript does not have
Each of these runs in PCRE. In JavaScript, as REGEX reports each one:
| Written | Means in PCRE | In JavaScript | Write instead |
|---|---|---|---|
(?i)abc | The whole pattern ignores case | Error: invalid group | The i flag: /abc/i |
(?>a+) | An atomic group, never backtracked into | Error: invalid group | No equivalent; rewrite so backtracking cannot help |
a++ | Possessive: as many as possible, never given back | Error: nothing to repeat | No equivalent; a+ usually does the job |
(?P<year>\d{4}) | A named group, Python’s spelling, which PCRE accepts | Error: invalid group | (?<year>\d{4}) |
\Aabc | abc at the start of the subject | No error: the letter A, then abc | ^abc, without the m flag |
abc\z | abc at the very end | No error: abc, then the letter z | abc$, without the m flag |
The last two are the dangerous ones. JavaScript accepts a backslash before a letter it gives no meaning to and reads the letter, so /\Aab/ matches the text “Aab” and not “ab” at the start, and nothing complains. Adding the u flag makes JavaScript refuse such escapes, which turns a silent wrong answer into an error you can see.
When the flavour was right: the escaping
Copied into code as a string, a pattern goes through two readers: the string, then the regular expression. The string takes the first backslash. new RegExp("\d+") is the pattern d+, which matches the letter d, and finds nothing in “2024”. Either double every backslash in the string, new RegExp("\\d+"), or write a literal, /\d+/, which is read once.
The flags are a separate argument: new RegExp("\\d+", "g"), not "/\\d+/g", which is a pattern with slashes and a g in it.
When it matches every other time: the g flag
A regular expression with the g flag remembers where its last match ended, in lastIndex, and test() starts from there. Called twice on the same text, /a/g.test("a") is true, then false, then true again. regex101 never shows this, because it runs the pattern once. Drop the g for a yes-or-no test, or set lastIndex back to 0.
When it works in the browser but not in Node
JavaScript’s regular expressions still grow. A group with flags of its own, (?i:abc), became part of the language in ES2025: current Chromium runs it, and Node 22 refuses it as an invalid group. A pattern that passes in one runtime can fail in another, so test it in the one that will run it.
Checking a pattern in the engine that will run it
- Paste the pattern into REGEX, which runs it in your browser’s own JavaScript engine, with the flags as a row of switches.
- Read the explanation under it. Each part is described in words, and syntax from another engine is named: an atomic group, Python’s named group, a possessive quantifier, flags written inside the pattern, and
\Aread as the letter A. - Paste the text it has to match, and watch the matches and groups. Replace mode shows what
$1and$<name>put back.
Questions people ask about regular expressions that work in regex101 and not in JavaScript
Why does my regex work on regex101 but not in JavaScript?
Usually because regex101 was on the PCRE flavour, which accepts syntax JavaScript does not have: inline flags, atomic groups, possessive quantifiers, \A and \z, and (?P<name>). Switch regex101 to ECMAScript, or check the escaping if you build the pattern from a string.
Why does a regex built from a string not match numbers?
The string reads the backslash first, so the pattern becomes d+, the letter d. Double it, new RegExp("\\d+"), or use a literal, /\d+/.
Does JavaScript support inline flags like (?i)?
Not for the whole pattern: (?i) at the start is an error, and the flags go after the closing slash or as the second argument. A group with its own flags, (?i:abc), was added in ES2025 and runs in current browsers, but older engines such as Node 22 refuse it.
Why does regex.test() return true, then false?
The g flag makes the expression remember where its last match ended, and test() starts from there the next time. Leave out g for a yes-or-no test, or reset lastIndex to 0.
What is \A in JavaScript regex?
Just the letter A. JavaScript gives \A no meaning and, without the u flag, reads it as the letter, so a pattern written for PCRE matches something else without an error. Use ^ for the start of the text.
Related tools
- JWT kid not found in the JWKSThe token names a signing key the published key set does not have: usually a key rotation, a stale cache, or another issuer.
- How to prove you did not use AI to write somethingNo test can prove a text was not written with AI. What you can show is how it was written: drafts, history and a timestamped record of the process.
- Why cron runs on days you did not ask forSet both day fields in a crontab and the job runs when either matches: the first Monday becomes every Monday and days 1 to 7.
- Why */7 in cron is not every 7 minutesA cron step restarts at :00 every hour, so */7 runs at :56 then :00, four minutes apart.
- Falsely accused of using AI: what to doAccused of using AI on work you wrote yourself? What a detector score does and does not show, what to ask for, and the evidence you can give.
What your files give away
A one-page checklist of what a photo, a document or a PDF says about you, free now. Then one letter a month, with one new tip.