Find All Regex Matches
How finding all regex matches works, what it does and does not change, Unicode and overlap pitfalls, and when plain text search is the better choice.
Finding all regex matches means running a regular expression across a whole string and collecting every place it matches, not just the first one. This review covers when that approach suits your task, what it does to your text, where results can become misleading or hard to reuse, and how to produce it in common environments. No tool-specific test results or measurements were available for this review, so it relies on documented language behavior and general principles. Where something depends on a specific tool or setup, that is noted as a gap.
Is finding all matches the right approach for your task?
Collecting every match is a good fit when you need to extract repeated items, such as identifiers, numbers, tags or log fields, or when you need to count occurrences and see where each one sits. The main benefit is precision: a single pattern can describe a family of strings that a literal search cannot, and you get every hit along with its position and any captured groups.
The deciding tradeoff is that a pattern is only as reliable as your understanding of the engine running it. Matches do not overlap by default, capture groups change what some functions return, character classes behave differently across regex flavors, and Unicode text can be split in places that look wrong to a reader. A pattern that looks correct in one tester can produce different results in your actual runtime.
Plain text search is preferable when you are looking for a fixed word or phrase, when the text contains characters with regex meaning that you would otherwise need to escape, or when the people maintaining the workflow are not comfortable reading patterns. A literal find, or a split on a known delimiter, is easier to audit and less likely to fail silently.
What finding all matches actually changes
Matching does not change the stored text. It reads the input and returns results: the matched substrings, their positions, and any captured groups. Any change to the text comes from a separate replace operation. This matters when evaluating a regex tester, because highlighted matches are a visual presentation layered over unchanged input; the highlight is not part of the text you copy out.
The word "all" is often ambiguous. In most engines it means all non-overlapping matches, scanned left to right, where each search resumes after the end of the previous match. It does not mean every possible way the pattern could match. In JavaScript, "all" is controlled by the g (global) flag; without it, many methods stop at the first match.
Non-overlapping behavior is easy to see with a short input. Searching "aaaa" for aa returns two matches, not three. To capture overlapping occurrences, a common technique is a zero-width lookahead containing a capture group:
// JavaScript
"aaaa".match(/aa/g);
// ["aa", "aa"]
[..."aaaa".matchAll(/(?=(aa))/g)].map(m => m[1]);
// ["aa", "aa", "aa"]
Capture groups also change what you get back. Python's re.findall returns the full matches only when the pattern has no groups; with one group it returns that group's text, and with several groups it returns tuples. This often surprises people who add parentheses for grouping and then lose the full match.
# Python
import re
text = "cat bat rat"
re.findall(r"\w+at", text)
# ['cat', 'bat', 'rat']
re.findall(r"(\w)at", text)
# ['c', 'b', 'r']
[m.group(0) for m in re.finditer(r"(\w)at", text)]
# ['cat', 'bat', 'rat']
Unicode: code units, code points and visible characters
What counts as one character depends on the engine. A reader sees one character, but the text may store several code points: an accented letter can be a base letter plus a combining mark, and many emoji are sequences joined by zero-width joiners. JavaScript strings are also indexed in UTF-16 code units, so a single emoji outside the Basic Multilingual Plane occupies two units.
// JavaScript
"😀".match(/./g).length; // 2 (surrogate halves, no u flag)
"😀".match(/./gu).length; // 1 (u flag: code points)
"e\u0301".match(/./gu).length; // 2 (e + combining acute accent)
Even with Unicode mode enabled, a dot matches one code point, not one visible character, so a combined accent or a multi-part emoji can be split across matches. Python's built-in re module also works on code points. If you need to match user-perceived characters, look for grapheme cluster support; the third-party Python regex module documents a \X token for this, while the standard re module does not provide it. Positions differ too: JavaScript match indices count UTF-16 code units, while Python indices count code points, so offsets from one environment should not be reused in the other without conversion.
Will the results stay readable and usable after copying or sharing?
Extracted matches are ordinary text, so they copy and paste like any other string. The risks are about context and presentation, not the characters themselves:
- Highlights do not travel. A tester's colored match markers exist only in that interface; if you share a screenshot or copy the input, recipients will not see which parts matched unless you export the match list.
- Color alone is not an accessible signal. If a tool indicates matches only by background color, people using screen readers or with low color perception may not perceive them. A textual list of matches with positions is the plain-text equivalent to share. Whether a particular tester exposes matches to assistive technology was not verified here.
- Split characters can corrupt output. A match boundary inside a surrogate pair or between a letter and its combining mark can produce broken or detached symbols when pasted elsewhere.
- Invisible characters can hide in results. Zero-width joiners, non-breaking spaces and bidirectional control characters may be captured and pasted without being visible, which can affect later searches, comparisons or display order. Inspect code points when results look right but compare as unequal.
- Positions are environment-specific. Reported offsets depend on whether the tool counts code units or code points, so treat them as valid only for the engine that produced them.
When you need to hand results to someone else, a plain list of matches, ideally with the pattern, flags and engine named, is more dependable than a highlighted view.
How to recognize the feature and where to find it
Clues that you are dealing with an all-matches search rather than a first-match search include a global flag (g) in the pattern options, a match count greater than one, a results list or table of matches with indices, and functions named findall, finditer, matchAll or scan. If a tool shows only one match where you expect several, the global option is the first thing to check.
| Environment | Method | What it returns |
| JavaScript | str.match(/pattern/g) | Array of full match strings, or null; groups are not included |
| JavaScript | str.matchAll(/pattern/g) | Iterator of match objects with groups and index; throws a TypeError if the g flag is missing |
| Python | re.findall(pattern, text) | List of strings, or group values or tuples when the pattern has groups |
| Python | re.finditer(pattern, text) | Iterator of match objects with full match, groups and positions |
| Command line (grep) | grep -o -E 'pattern' file | Each matched part printed on its own line |
Useful search terms include "regex global flag", "find all regex matches", "matchAll", "findall vs finditer" and "grep only matching". When choosing an online regex tester, confirm that it lets you select the same flavor as your runtime (for example JavaScript, PCRE, Python or .NET), because syntax support and Unicode handling differ between them. Check your own language and tool documentation for exact behavior; the table above reflects widely documented standard behavior, not tests of any particular version.
Before pasting logs, credentials or personal data into a web-based tester, find out how it handles input. A tool's appearance or claims such as "runs locally" are not evidence of its data practices, and no privacy information about specific testers was available for this review. For sensitive text, running the pattern in a local script or command-line tool avoids the question.
Bottom line
Finding all regex matches is well suited to extracting or counting repeated patterns, provided you know your engine's rules: matches are non-overlapping by default, groups can change the return shape, and Unicode text may be split below the level of visible characters. Share results as a plain list with the pattern and flavor stated, and fall back to literal search when the target is a fixed string or the pattern would be harder to maintain than the problem warrants.