npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

glob-to-regex.js

v1.2.0

Published

Transform GLOB patterns to JavaScript regular expressions for fast file path matching.

Readme

glob-to-regex.js

Transform GLOB patterns to JavaScript regular expressions for fast file path matching.

This tiny library converts familiar shell-style glob patterns like **/*.ts or src/{a,b}/**/*.js into JavaScript RegExp objects and provides a convenient matcher utility.

Install

yarn add glob-to-regex.js
# or
npm i glob-to-regex.js

Quick start

import {toRegex, toMatcher} from 'glob-to-regex.js';

// Build a RegExp from a glob
const re = toRegex('src/**/test.ts');
re.test('src/a/b/test.ts'); // true
re.test('src/test.ts');     // true
re.test('src/test.tsx');    // false

// Build a predicate function from a pattern or an array of patterns
const match = toMatcher(['**/*.ts', '!**/*.d.ts']); // negative patterns are not special; use a RegExp if needed
match('index.ts');    // true
match('types.d.ts');  // true (negation is not parsed specially)

API

  • toRegex(pattern: string): RegExp

    • Converts a glob pattern to an anchored regular expression (^...$).
  • toMatcher(pattern: string | RegExp | Array<string | RegExp>): (path: string) => boolean

    • Accepts a glob string, a RegExp, or an array of them. If given an array, it returns true if any item matches (logical OR, short-circuited).
    • Strings starting with / and ending with /flags? are treated as regular expressions (e.g. "/\\.test\\.ts$/").

Supported glob features

  • / separates path segments
  • * matches zero or more characters within a single segment (does not cross /)
  • ? matches exactly one character within a single segment
  • ** matches across path segments, including none
  • {a,b,c} alternation groups (no nesting). Each item inside can itself contain glob syntax
  • Character classes: [abc], [a-z], [!a-z], [!abc]
  • Extended globbing (when extglob: true option is set):
    • ?(pattern-list) matches zero or one occurrence of the given patterns
    • *(pattern-list) matches zero or more occurrences of the given patterns
    • +(pattern-list) matches one or more occurrences of the given patterns
    • @(pattern-list) matches exactly one of the given patterns
    • !(pattern-list) matches anything except one of the given patterns
    • Pattern lists use | as separator (e.g., @(jpg|png|gif))

Notes:

  • The produced RegExp is anchored at start and end (^...$).
  • Character classes are copied through to the output regex. Use standard JavaScript class syntax.
  • Brace groups are not nestable. If an unmatched { is found, it is treated literally.

Examples

toRegex('a/b/c.txt').test('a/b/c.txt'); // true
toRegex('a/*.txt').test('a/file.txt');  // true
toRegex('a/*.txt').test('a/x/y.txt');   // false
toRegex('file?.js').test('file1.js');   // true
toRegex('src/**/test.ts').test('src/a/b/test.ts'); // true
toRegex('assets/**').test('assets/a/b.png');       // true
toRegex('*.{html,txt}').test('page.html');         // true
toRegex('src/{a,b}/**/*.ts').test('src/b/x/y.ts'); // true
toRegex('file[0-9].txt').test('file5.txt');        // true
toRegex('file[!0-9].txt').test('filea.txt');       // true
toRegex('**/*.[jt]s{,x}').test('dir/a/b.jsx');     // true

// Extended globbing examples
toRegex('file?(s).txt', {extglob: true}).test('file.txt');  // true
toRegex('file?(s).txt', {extglob: true}).test('files.txt'); // true
toRegex('file.@(jpg|png|gif)', {extglob: true}).test('file.jpg'); // true
toRegex('/var/log/!(*.gz)', {extglob: true}).test('/var/log/syslog'); // true
toRegex('/var/log/!(*.gz)', {extglob: true}).test('/var/log/error.log.gz'); // false
toRegex('src/**/!(*.test).js', {extglob: true}).test('src/app.test.js'); // false
toRegex('src/**/!(*.test).js', {extglob: true}).test('src/index.js'); // true

TypeScript

Types are bundled. The library targets modern Node.js and browsers.

Performance

toRegex performs a single pass over the pattern and creates a native RegExp. Matching is then performed by V8's highly optimized engine.

Limitations

  • Brace groups are not nested.
  • Negated globs like !**/*.d.ts are not parsed specially. If you need exclusion, combine multiple matchers or filter results separately.

License

Apache-2.0 © streamich