Regex Match Counter

How many times does this pattern appear? This tool answers that instantly: paste a regex and some text and it counts every match, lists each one with its character position and length, and tallies how often each distinct value shows up. It is the quick way to verify a pattern hits the right number of times before you wire it into code.

Global matching is always on.

How to use the Regex Match Counter

Type a pattern and paste your text. The headline number shows the total count of matches; below it, the left panel lists every match in order with its starting character index and length, and the right panel tallies how many times each distinct matched string occurs, sorted from most to least frequent.

The flag checkboxes are the standard JavaScript regex flags — i (ignore case), m (multiline anchors), s (dot matches newlines), and u (Unicode). Global matching is always on, because counting only makes sense across the whole input. The tool guards against zero-width patterns that could otherwise match endlessly, so an empty or always-empty match is reported rather than looping.

It all runs locally in your browser and updates as you type, so you can paste logs, source code, or any sensitive text without it leaving your machine.

Counting matches: the subtle parts

Counting how often a pattern occurs sounds trivial, but a few details trip people up. The first is the difference between the total number of matches and the number of distinct matches. A pattern that finds email addresses in a paragraph might report eight matches but only five unique addresses, because some repeat. Both numbers are useful: the total tells you how many times something appears, while the distinct tally tells you how many different values exist and which are most common. This tool shows both side by side so you never have to guess which one a single number represents.

The second subtlety is global matching. In JavaScript a regex without the g flag returns only the first match, so any count based on it would be wrong. Counting always uses global iteration, walking the string from the end of one match to find the next, which is why overlapping matches are not counted twice — each character is consumed once. If you need overlapping occurrences you generally restructure the pattern with lookahead, since standard matching is non-overlapping by design.

The third is the zero-width match trap. A pattern like a* or ^ can match an empty string at a position without consuming any characters. A naive counting loop that does not advance past an empty match will spin forever. Robust counting detects a zero-length match and steps forward by one position, which is exactly what the modern matchAll iterator does internally. Knowing the position and length of each match — shown here in the ordered list — also helps you confirm that a pattern is anchoring where you expect rather than catching extra or partial text, which is often the real reason a count comes out higher or lower than intended.

Common use cases

  • Verifying a pattern. Confirm a regex matches exactly as many times as you expect before using it in code.
  • Counting occurrences. Tally how many times a word, tag, or token appears in a document or log.
  • Finding duplicates. Use the distinct tally to see which matched values repeat and how often.
  • Locating matches. Read each match's index to check where in the text the pattern is firing.

Frequently asked questions

What is the difference between the two panels?

The left panel lists every match in order with its position; the right panel groups identical matches and counts each, sorted by frequency. The headline number is the total match count.

Does it count overlapping matches?

No. Like standard regex engines, matching is non-overlapping — each character is consumed once. To find overlapping occurrences you would use lookahead in the pattern.

Why does an empty pattern report a special message?

Patterns that can match an empty string (zero-width) would loop forever in a naive counter. The tool advances past empty matches and notes when a pattern matches nothing meaningful.

Are capture groups counted?

The count is of whole matches, not groups. Each occurrence of the overall pattern counts once regardless of how many groups it contains.