Glob to Regex
The `glob-to-regex.js` library, currently stable at version 1.2.0, provides a lightweight and performant solution for converting shell-style glob patterns into native JavaScript `RegExp` objects. It is actively maintained, with recent feature releases in late 2025, demonstrating a consistent development cadence. Key differentiators include its focus on performance by leveraging V8's optimized regex engine for matching, the provision of both a direct `toRegex` function for `RegExp` generation and a `toMatcher` utility for predicate functions, and comprehensive support for various glob features including extended globbing (enabled via an option since v1.2.0) and character classes. The package ships with first-class TypeScript types, ensuring a robust developer experience. Unlike some alternatives, it explicitly anchors all generated regular expressions, which is crucial for precise full-path matching, though this can be a behavioral nuance for new users. Its small footprint makes it suitable for environments where bundle size is a concern.
Common errors
-
Error: Cannot find module 'glob-to-regex'
cause Attempting to import the package using the old name without the '.js' suffix.fixChange the import path to 'glob-to-regex.js'. -
TypeError: Invalid regular expression: /.../: Invalid character class
cause The glob pattern contains a malformed or unescaped character class that JavaScript's `RegExp` engine cannot parse.fixEnsure character classes in glob patterns (e.g., `[a-z]`, `[!0-9]`) are syntactically correct and don't conflict with JavaScript regex rules. The library copies them directly. -
My glob pattern `*.js` isn't matching `src/file.js`.
cause The `toRegex` function produces anchored regular expressions (`^...$`), meaning `*.js` only matches files directly in the root, not in subdirectories.fixUse the `**` pattern for recursive matching, e.g., `**/*.js` to match files in any subdirectory.
Warnings
- gotcha The package name was changed in v1.0.1 from 'glob-to-regex' to 'glob-to-regex.js'. Ensure your import paths reflect the `.js` suffix, especially for ESM environments, to avoid module resolution errors.
- gotcha The `toMatcher` function does not specially parse 'negative patterns' (e.g., `!**/*.d.ts`). Such patterns are treated as standard glob patterns, meaning `toMatcher(['!foo.ts'])` will match 'foo.ts' if no other positive pattern prevents it.
- gotcha Generated regular expressions are always implicitly anchored at the start and end (`^...$`). This means `toRegex('*.js')` will match `file.js` but not `path/to/file.js`.
- gotcha Brace groups (e.g., `{a,b,c}`) are not nestable. If nested braces are encountered, they will likely be treated as literal characters or result in unexpected regex syntax.
- gotcha Support for extended globbing features (like `?(pattern-list)`, `*(pattern-list)`) was added in v1.2.0 but requires the `extglob: true` option to be explicitly set. They are not enabled by default.
Install
-
npm install glob-to-regex.js -
yarn add glob-to-regex.js -
pnpm add glob-to-regex.js
Imports
- toRegex
import { toRegex } from 'glob-to-regex'; const toRegex = require('glob-to-regex.js').toRegex;import { toRegex } from 'glob-to-regex.js'; - toMatcher
import { toMatcher } from 'glob-to-regex'; const toMatcher = require('glob-to-regex.js').toMatcher;import { toMatcher } from 'glob-to-regex.js'; - glob-to-regex.js (module)
const GlobToRegex = require('glob-to-regex');import * as GlobToRegex from 'glob-to-regex.js';
Quickstart
import {toRegex, toMatcher} from 'glob-to-regex.js';
// Build a RegExp from a glob
const re = toRegex('src/**/test.ts');
console.log(`'src/a/b/test.ts' matches 'src/**/test.ts': ${re.test('src/a/b/test.ts')}`); // true
console.log(`'src/test.ts' matches 'src/**/test.ts': ${re.test('src/test.ts')}`); // true
console.log(`'src/test.tsx' matches 'src/**/test.ts': ${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
console.log(`'index.ts' matches ['**/*.ts', '!**/*.d.ts']: ${match('index.ts')}`); // true
console.log(`'types.d.ts' matches ['**/*.ts', '!**/*.d.ts']: ${match('types.d.ts')}`); // true (negation is not parsed specially)
// Demonstrate extended globs (requires option)
const reExt = toRegex('file.@(jpg|png|gif)', {extglob: true});
console.log(`'file.jpg' matches 'file.@(jpg|png|gif)' with extglob: ${reExt.test('file.jpg')}`); // true
console.log(`'file.txt' matches 'file.@(jpg|png|gif)' with extglob: ${reExt.test('file.txt')}`); // false
// Demonstrate anchored regex behavior
const reAnchored = toRegex('*.js');
console.log(`'my-file.js' matches '*.js': ${reAnchored.test('my-file.js')}`); // true
console.log(`'path/to/my-file.js' matches '*.js': ${reAnchored.test('path/to/my-file.js')}`); // false (due to implicit anchoring)