Settings

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:

WrittenMeans in PCREIn JavaScriptWrite instead
(?i)abcThe whole pattern ignores caseError: invalid groupThe i flag: /abc/i
(?>a+)An atomic group, never backtracked intoError: invalid groupNo equivalent; rewrite so backtracking cannot help
a++Possessive: as many as possible, never given backError: nothing to repeatNo equivalent; a+ usually does the job
(?P<year>\d{4})A named group, Python’s spelling, which PCRE acceptsError: invalid group(?<year>\d{4})
\Aabcabc at the start of the subjectNo error: the letter A, then abc^abc, without the m flag
abc\zabc at the very endNo error: abc, then the letter zabc$, 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

  1. Paste the pattern into REGEX, which runs it in your browser’s own JavaScript engine, with the flags as a row of switches.
  2. 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 \A read as the letter A.
  3. Paste the text it has to match, and watch the matches and groups. Replace mode shows what $1 and $<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

More on this

All 8 guides under “Showing the work is yours”.

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.

No spam, unsubscribe at any time, nothing is shared.