Tutorial TypeScript Security

How to Match Globs Without Compiling a Regular Expression

Use matchnext from source, learn the real isMatch and match APIs, and compare a pathological glob against picomatch 4.0.7 using measurements from one machine.

Dao Quang Truong
9 min read
On this page tap to expand

Most JavaScript glob matchers turn your pattern into a RegExp and then test the path. That is fast for ordinary patterns such as **/*.js. It is a problem when a pattern compiles to nested, unbounded quantifiers: the regex engine can backtrack so hard that a short string ties up the event loop.

This tutorial shows you how to use matchnext, a TypeScript glob matcher that never constructs a RegExp at runtime and caps work with a step budget. You will run the real public API, compare it with picomatch 4.0.7 on one pathological pattern, and see where the project README and the code disagree.

The examples were run on 28 November 2026, with Bun 1.4.4 for matchnext and Node.js 24.22.1 for picomatch 4.0.7. Times are from that single machine and that single run. They are not a benchmark suite, and they are not copied from the repository README. If you rerun the same commands, expect the milliseconds to move around; the useful signal is the shape of the curve, not a single cell.

What You Will Learn

  • How to run matchnext from the repository (it is not on the npm registry)
  • The real isMatch and match APIs, including dotfiles, braces, and extglobs
  • Why a pattern such as +(*a)*b can get expensive in a regex-based matcher
  • How a step-budgeted, zero-RegExp engine behaves on the same input
  • What this does — and does not — mean for CVE-2026-112366

Prerequisites

  • Bun 1.4+ (the repo’s tests and source imports are set up for Bun)
  • Git

matchnext 0.0.0-m1 is marked "private": true and is not published to npm. Clone it and import the source:

git clone https://github.com/2tsoftwareapp/matchnext.git
cd matchnext
bun install
bun test ./test

On this machine, bun test ./test reported 26 passing tests and 168 expect() calls, which matches the current README. The package has no main or exports field yet, so the supported way to try the API is to import ./src/index.ts from Bun, or to work inside the clone.

Create glob-demo.ts in the repo root:

import { isMatch, match } from './src/index.ts';

console.log(isMatch('a/b/c', 'a/**/c'));      // true
console.log(isMatch('a/b/c.js', '**/*.js'));  // true

Run it with bun glob-demo.ts.

Use the API That Is in the Source

The implementation lives in a single file, src/index.ts. Two functions are exported.

isMatch(input, patterns, options?) returns whether one string matches a pattern or any pattern in an array:

isMatch('index.html', ['*.html', '*.htm']); // true
isMatch('foo', '!(bar)');                   // true
isMatch('bar', '!(bar)');                   // false
isMatch('README.MD', '*.md', { nocase: true }); // true
isMatch('foo.js', '*.{js,ts}');             // true

An empty pattern throws TypeError: Expected pattern to be a non-empty string. Leading ./ is stripped from patterns, the same compatibility behavior picomatch uses.

match(list, patterns, options?) filters an array of strings. This is the function in the source. It is not “return the matched portion of one input, or null,” which is what the README’s API table says as of this writing. Prefer the source:

match(['a.js', 'b.md', 'c.js'], '**/*.js');
// [ 'a.js', 'c.js' ]

Options in the MatchOptions type are dot, separator (default '/'), noExtglob, noBraces, and nocase. The default separator in the code is a slash. Do not copy a README line that shows a different default without checking DEFAULT_SEPARATOR.

Dotfiles Follow the Pattern, Not a Global Ban

Wildcards do not match a leading dot unless you pass { dot: true }. A pattern that itself starts with a dot still matches:

isMatch('.gitignore', '*');                  // false
isMatch('.gitignore', '*', { dot: true });   // true
isMatch('.gitignore', '.*');                 // true

That last line is easy to get wrong. .* contains a literal dot, so it matches .gitignore without dot: true. The unit tests in test/core.test.ts lock this down (*.md does not match .c.md; .*.md does). If a README example says otherwise, trust the tests.

Why Some Globs Get Expensive

picomatch, the matcher behind a large part of the JavaScript glob ecosystem, compiles patterns to regular expressions. Extglobs such as +(...) become quantified groups. If a group contains a wildcard and is then quantified again, the resulting regex can backtrack exponentially on a short string that almost matches.

A concrete pattern used in the matchnext README is +(*a)*b. The input is a string of a characters and no trailing b, so the match should fail. On picomatch 4.0.7, isolated runs on this machine were:

Input lengthpicomatch 4.0.7 (+(*a)*b)
101.6 ms
153.9 ms
2022 ms
2295 ms
25684 ms
285,343 ms

Each row is one Node.js process. The jump from 25 to 28 characters is the story: a few extra as turned a sub-second miss into a multi-second miss. That is not “the matcher is slow on large trees.” The input is 28 characters long.

You can reproduce the last row yourself, in a throwaway CommonJS file, after npm pack picomatch@4.0.7 (or any install of that exact version):

const pm = require('picomatch');
console.time('picomatch');
console.log(pm.isMatch('a'.repeat(28), '+(*a)*b'));
console.timeEnd('picomatch');

Give the process a generous timeout if you increase n. This article stopped at 28.

A note on naming: GitHub published advisory GHSA-fc5q-39xc-vjpf / CVE-2026-112366 about this class of picomatch patterns. The advisory was withdrawn on 18 November 2026. GitHub’s advisory page now says it is not a security issue, and a picomatch maintainer pointed at that withdrawal. This tutorial does not treat the withdrawn CVE as a current official vulnerability, and it does not repeat CVSS scores from older write-ups. The engineering fact remains: some patterns make a regex-based matcher arbitrarily expensive, which matters if untrusted input can influence the glob.

How matchnext Avoids That Path

matchnext does not compile a pattern to RegExp. The pipeline in src/index.ts is:

  1. Expand braces such as {js,ts} or {1..3}
  2. Lex the pattern into tokens (char, star, globstar, qmark, class, extglob groups, separators)
  3. Match with memoized recursive descent over Unicode code points

The matcher counts steps. The cap is 4_000_000 + 400 * input.length * 40. When the cap is exceeded, the code throws Error: matchnext: pattern complexity limit exceeded. A thrown budget is a failed match path for isMatch only if the caller swallows it; the public isMatch function currently lets that error propagate, so a truly huge pattern can surface as an exception rather than a quiet false. Ordinary patterns, including the ones below, return a boolean.

On the same +(*a)*b inputs, matchnext (Bun, first-process-style calls in one script) returned false in a few milliseconds at length 25 and under 20 ms at length 100:

Input lengthmatchnext +(*a)*b
255.5 ms
280.6 ms
304.1 ms
5016 ms
10019 ms

The 0.6 ms row is a later call in the same process and is probably warmer than the 5.5 ms first call. Do not read the table as a precise microbenchmark. Read it as: the same pattern that took picomatch 5.3 seconds at length 28 returned quickly here, and stretching the input to 100 characters did not produce a multi-second hang.

A longer constructed pattern from the README, "+" + "(*a)".repeat(10) + "*b" against 100 as, returned false in about 50 ms in the same session. That is slower than the README’s “under 10 ms” wording, and it is still in a different class from a multi-second event-loop stall.

const pattern = '+' + '(*a)'.repeat(10) + '*b';
const input = 'a'.repeat(100);

console.time('matchnext');
console.log(isMatch(input, pattern)); // false
console.timeEnd('matchnext');

Limits You Should Know Before You Depend on It

matchnext is an early milestone (0.0.0-m1). The README is explicit that the full upstream picomatch fixture corpus is not yet wired through an adapter, even though a smaller baseline suite already runs. If you need drop-in picomatch compatibility in production today, this is a library to evaluate, not a finished replacement.

Install and API documentation are still catching up:

  • There is no npm package. Plan on a git clone.
  • Trust src/index.ts and test/core.test.ts over README snippets when they conflict (match’s signature, the .* dotfile example, the default separator).
  • isMatch throws on an empty pattern. Guard user-supplied strings before you call it.
  • A step-budget exhaustion currently throws rather than returning false. If you accept patterns from users, wrap the call.

The engine implements *, ?, [...], braces, **, and the extglob forms !(), +(), @(), *(), and ?(). It decides whether a string matches a glob. It does not walk the file system.

Conclusion

If every glob you run is a literal you wrote, such as src/**/*.ts, a regex-based matcher is usually fine. The failure mode to remember is a pattern that compiles to nested unbounded quantifiers, especially if that pattern can come from a config file, a plugin, or a user. On picomatch 4.0.7, +(*a)*b against 28 as took more than five seconds to return false in the run above.

matchnext attacks that class of problem in the engine: no runtime RegExp, Unicode code-point matching, and a polynomial step cap. It is not on npm yet, and some README examples are out of date, but the public functions are small and the tests are runnable. Clone the repo, run bun test ./test, then try isMatch on the patterns you actually use. If you also time picomatch on +(*a)*b, write down the Node version and the input length. A few characters change the result more than a marketing table will tell you.

The source is at github.com/2tsoftwareapp/matchnext. For the withdrawn advisory, see GHSA-fc5q-39xc-vjpf.

Related Articles