glob-to-regexp

A tiny, dependency-free JavaScript library that converts wildcard glob patterns into native RegExp objects, with optional bash-style extended and globstar matching.

Library
npm
v0.4.1
150stars
BSD-2-Clause

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
30/100Needs Attention
Development Activity0
Maintenance0
Community48
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
50/100Fair
Architecture60
Code Quality45
Innovation60
Learning Curve35

glob-to-regexp turns a wildcard glob string such as *.min.js into a native JavaScript RegExp object, so any code that already knows how to work with regular expressions can match against glob-style patterns without pulling in a full glob-walking engine. Basic * wildcards are supported out of the box, with two opt-in modes: extended for bash-style ? single-character matches, [a-z] character ranges, and {a,b} alternation groups, and globstar for node-glob-compatible ** path-segment semantics.

The entire implementation is a single, dependency-free file, which is likely why it sees tens of millions of weekly downloads as a small transitive dependency inside larger build-tool and bundler dependency trees rather than as something most developers install directly.

What You Get

  • A single default export function, globToRegExp(glob, opts), that returns a native RegExp
  • Basic wildcard * matching, translated to .* (or [^/]* per segment when globstar is enabled)
  • An extended option enabling bash-style ?, [abc] character ranges, and {a,b} alternation groups
  • A globstar option implementing ** path-segment semantics compatible with node-glob
  • A flags option that passes RegExp flags (e.g. 'g', 'i') straight through to the constructed pattern

Common Use Cases

  • Filtering file paths or URLs against a wildcard pattern without pulling in a full glob-matching engine
  • Powering file-watch matching inside build tools that need a compiled RegExp rather than a stateful matcher
  • Validating whether an asset URL (e.g. *.min.js) matches a bundler or CDN naming convention
  • Implementing simple user-facing search or filter inputs that accept * wildcards

Under The Hood

Architecture glob-to-regexp is a single-function, single-file module (index.js) with no internal layering — the entire conversion happens inside one loop that walks the glob string character by character and appends to a growing regex string via a switch statement. Control flow relies on intentional switch-case fallthrough (the [, ], {, }, and , cases fall through toward the extended-mode branch and finally to a default escape), which is compact but requires care to read since a missing break looks like a bug rather than a deliberate ladder. State is minimal — a handful of booleans (extended, globstar, inGroup) plus the accumulating string — and there is no dependency injection or exposed internals to override; the entire surface area that could break is this one function behind a single CommonJS export.

Tech Stack The package has zero runtime dependencies and no build step — it ships the raw CommonJS index.js directly as main, targeting any JavaScript environment with a native RegExp and no transpilation required. package.json declares only a test script (node test.js) built on Node’s core assert module rather than a test framework, and CI is configured via a long-dormant .travis.yml targeting Node 0.8/0.10, reflecting the library’s age (created 2013, last pushed 2019) rather than active tooling investment.

Code Quality test.js is an extensive, if unconventional, regression suite — many assertMatch/assertNotMatch calls covering wildcard, extended, and globstar modes, exercised twice across globstar settings plus dedicated globstar-only cases. There are no TypeScript types, no JSDoc annotations, and no linter or formatter configuration in the repo; the only runtime validation is a single TypeError thrown when the input isn’t a string, and option values are silently coerced with !! rather than validated.

API Design The public API is a single function, globToRegExp(glob, opts), returning a plain RegExp — there is no class to instantiate and no builder chain, and calling it with just a glob string works with sensible defaults. The small opts object (extended, globstar, flags) is documented with runnable examples in the README, so a new consumer can be productive from the README alone; the tradeoff is that the return value is a bare RegExp with no bundled TypeScript typings, so editor autocomplete for opts comes only from community @types packages, not the library itself.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search