How do Python and JavaScript regexes differ when finding multiple matches?
Quick answer
Python’s re.findall() returns all non-overlapping matches by default. JavaScript’s text.match() returns only the first match unless the regular expression has the g flag; with g, it returns all complete matches. For a pattern without capture groups, both can return a list or array of matched strings. Capture groups change Python’s findall() results, while global JavaScript match() omits capture groups. [S1] [S2]
The question, the details, the next step.
A practical checklist
Decide whether you need complete matches, captured text, or match details such as positions.
In JavaScript, add the
gflag when usingtext.match()to collect all complete matches.In Python, check whether capturing groups change
findall()from full matches to captured values or tuples.Try the pattern on text with several matches and on text with no matches.
Choose Python
finditer()or JavaScriptmatchAll()/exec()when their match-object or capture-group behavior fits your task.
A few more useful answers
Does Python `findall()` return overlapping matches?
No. It returns all non-overlapping matches. [S1] The cited reference excerpt does not specify a solution for finding overlaps.
What does JavaScript `match()` return when nothing matches?
It returns null. [S2] Check for that before treating the result as an array.
Which Python option helps when I need match positions?
Python’s re.finditer() yields match objects and is useful when you need positions or match details. [S1]
Sources
References used for this guide.
- [S1] Python 3.15.2 re — Regular expression operations docs.python.org · Checked
- [S2] String.prototype.match() — JavaScript | MDN developer.mozilla.org · Checked
