Regex Cheat Sheet: JavaScript, Python and PCRE2 Syntax Side by Side
Checked against Regular expressions (JS, Python re, PCRE2) Node.js 24.4 (V8), Python 3.12 tested and 3.15 docs, PCRE2 10.49 on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers who write the same pattern in more than one language and keep getting bitten by the small differences. Rows marked JS, Py or PCRE only work in that flavour; unmarked rows behave the same in all three. Every JS and Python example was run in Node.js 24.4 and Python 3.12.8, and PCRE2 rows were checked against the 10.49 documentation only.
When to use what
Start with the flavour your code actually runs in, not the one your editor highlights. JavaScript is the strictest of the three: no possessive quantifiers, no atomic groups, no \A or \z, and \w is ASCII-only even with the u flag. Python's re sits in the middle: it gained possessive quantifiers and atomic groups in 3.11, but it still has no \p{...} classes and its lookbehind must be fixed width. PCRE2 (PHP, grep -P, nginx, many editors) has the most features.
My rule of thumb: if a pattern has to run in more than one place, stick to the common subset (classes, anchors with the m flag, lazy quantifiers, lookahead, non-capturing groups) and write named groups in the target language's own syntax. Reach for possessive quantifiers or atomic groups only when you have measured a backtracking problem. Use \p{L} in JS with the u or v flag when you need letters beyond ASCII; in Python, the default Unicode \w is often enough, and the third-party regex module covers the rest.
Paste a pattern into the regex tester to see matches live, or the regex explainer to get it read back token by token. The regex patterns guide has ready-made patterns for emails, dates and URLs.
The big trap here is \w: Python matches accented letters by default, JavaScript never does.
\dMatch one digit; Python also matches non-ASCII digits unless you pass re.ASCII
\wMatch a word character; JS stops at é ("héllo" splits in two), Python keeps it
\sMatch one whitespace character, including tab and newline
[^;]+Match a run of anything except a semicolon, handy for splitting key=value pairs
.Match any character except a newline; add the s flag (re.S) to include newlines
\h \RPCRE: match horizontal whitespace, or any newline sequence including CRLF
In JavaScript, \A is not an anchor: it silently matches a literal capital A.
^ and $Match the start and end of the string, or of each line with the m flag
\bMatch a word boundary so cat matches "cat" but not the cat inside "concat"
\APy and PCRE: match only at the very start of the string, ignoring the m flag
\zPCRE and Python 3.14+: match only at the very end of the string
Python 3.12 raises "bad escape \z"
\ZPCRE: end of string or before a final newline. Python: end of string only
re.fullmatch(r"\d+", s)Py: require the whole string to match; unlike $ it rejects a trailing newline
* + ? {2,4}Repeat zero or more, one or more, optional, or between 2 and 4 times
<.+?>Lazy: stop at the first closing bracket, giving "<a>" and "<b>" instead of one big match
a*+ \d++Py 3.11+ and PCRE: possessive, never gives characters back; a*+a fails on "aaaa"
JS: SyntaxError "Nothing to repeat"
(?>...)Py 3.11+ and PCRE: atomic group, drops backtracking points once the group matches
JS: SyntaxError "Invalid group"
(?:ab)+Group without capturing, so match arrays stay clean
(?<y>\d{4})JS and PCRE: named capture; read it from match.groups.y
(?P<y>\d{4})Py and PCRE: named capture; read it from m.group("y") or m.groupdict()
JS rejects it; Python 3.12 rejects (?<y>)
\k<q> (?P=q)Reuse what a named group matched: \k<q> in JS and PCRE, (?P=q) in Py and PCRE
"$<d>/$<m>/$<y>" r"\g<d>/\g<m>/\g<y>"Reorder a date with named groups in replace() (JS) or re.sub() (Py)
JavaScript allows any lookbehind, Python only fixed width, PCRE2 10.43+ bounded width.
\d+(?=USD)Lookahead: match a number only when USD follows it, without consuming USD
\d+(?!\d|USD)Negative lookahead: match a number not followed by USD (the \d stops partial matches)
(?<=\$)\d+Lookbehind: match the amount after a dollar sign, not the sign itself
(?<!\$)\dNegative lookbehind: match a digit that is not preceded by a dollar sign
(?<=ab+)\dVariable-length lookbehind: works in JS; Python raises "look-behind requires fixed-width pattern"
foo\KbarPCRE: reset the match start so only "bar" is reported, a cheap lookbehind
i re.I (?i)Ignore case; Python and PCRE also take it inline at the start of the pattern
a(?i:b)cTurn on case-insensitivity for one part only; works in Node 24 and Python
Matches "aBc" but not "ABc"
m re.MMake ^ and $ match at every line break
s re.SLet the dot match newlines too
gJS: find all matches; required for matchAll and replaceAll with a regex
yJS: sticky, match only at lastIndex; useful for hand-written tokenizers
dJS: add match.indices with start and end offsets for each group
re.X (?x)Py and PCRE: verbose mode, ignore whitespace and allow # comments in the pattern
re.APy: make \w, \d, \s and \b ASCII-only, like JavaScript
Python's re has no \p{...}; it raises "bad escape \\p". Use the third-party regex module instead.
/\p{L}+/guJS: match runs of letters in any script, so "Ωmega" and "é" both match
/\p{Script=Greek}+/guJS: match only Greek letters
\p{L} \p{Greek}PCRE: same idea; a bare script name means the script extension
/[\p{L}--[a-z]]/vJS v flag: subtract ASCII lowercase from all letters
/[\p{Script=Greek}&&\p{Ll}]/vJS v flag: intersect two sets, here lowercase Greek letters only
/\p{RGI_Emoji_Flag_Sequence}/vJS v flag: match a whole two-character flag emoji as one unit
RegExp.escape("1+1=2")JS: escape user input before building a RegExp from it
re.escape("a.b*c")Py: escape special characters; returns a\.b\*c
[...s.matchAll(/[a-z](\d)/g)]JS: loop over every match with its groups; throws without the g flag
re.findall(r"[a-z](\d)", s)Py: return only the group contents ["1", "2"] when the pattern has one group
re.match vs re.searchPy: match anchors at position 0, search scans the whole string
Worked example: Pull error lines out of a log in JS and Python
Say you have three log lines and want only the errors, with the service and optional user id as fields.
const re = /^(?<ts>\S+) ERROR \[(?<svc>\w+)\] (?<msg>.+?)(?: user=(?<user>\d+))?$/gm;
[...log.matchAll(re)].map(m => ({ ...m.groups }));
pat = r'^(?P<ts>\S+) ERROR \[(?P<svc>\w+)\] (?P<msg>.+?)(?: user=(?P<user>\d+))?$'
[m.groupdict() for m in re.finditer(pat, log, re.M)]
Both return two records: {ts: "2026-11-28T09:14:02Z", svc: "api", msg: "timeout", user: "42"} and the matching db / deadlock / 7 line. The INFO line is skipped because the literal word ERROR must follow the timestamp. The m flag matters: without it ^ and $ only see the start and end of the whole log, and you get nothing. The lazy .+? stops the message before the optional user part; with a greedy .+ the message would swallow user=42 and the user group would be empty. The only change between languages is the group syntax, (?<ts>) against (?P<ts>).
Common mistakes
Calling test() twice on a regex with the g flag and getting true, then false.
A global regex keeps lastIndex between calls. Drop the g flag for yes/no checks, or reset lastIndex to 0.
Building a pattern from user input with new RegExp(input), so a dot or plus changes the meaning.
Wrap the input in RegExp.escape() in JS or re.escape() in Python first.
Using \A or possessive quantifiers in JavaScript after copying a pattern from PHP.
JS treats \A as a literal A and rejects *+ outright. Use ^ without the m flag, and rewrite possessive parts with a lookahead and backreference if you really need them.
Writing \p{L} in Python's re and getting a bad escape error.
Use [^\W\d_] for any Unicode letter, or install the regex module, which supports property classes.
Assuming $ means end of string in Python, then accepting "123\n" as a valid number.
Use re.fullmatch() or end the pattern with \Z (or \z on 3.14+), which do not allow a trailing newline.
Using a greedy .* between two delimiters and matching from the first to the last one.
Use the lazy .? or, faster, a negated class such as [^>] that cannot run past the delimiter.
Related cheat sheets
Sources
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/escape
- https://docs.python.org/3/library/re.html
- https://www.pcre.org/current/doc/html/pcre2pattern.html
- https://github.com/PCRE2Project/pcre2/releases
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.