Registry / serialization / glob-to-regex.js

glob-to-regex.js

JSON →
library1.2.0jsnpmunverified

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.

npm install glob-to-regex.js
INSTALL
IMPORT
SIG · GLOB-TO-REGEX.JS
G
glob-to-regex.js
serializationjavascriptv1.2.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

toRegex
✓ import { toRegex } from 'glob-to-regex.js';
✗ import { toRegex } from 'glob-to-regex'; const toRegex = require('glob-to-regex.js').toRegex;
The package name in import paths requires the '.js' suffix for ESM. CommonJS usage needs `.toRegex` property access.
toMatcher
✓ import { toMatcher } from 'glob-to-regex.js';
✗ import { toMatcher } from 'glob-to-regex'; const toMatcher = require('glob-to-regex.js').toMatcher;
The package name in import paths requires the '.js' suffix for ESM. CommonJS usage needs `.toMatcher` property access.
glob-to-regex.js (module)
✓ import * as GlobToRegex from 'glob-to-regex.js';
✗ const GlobToRegex = require('glob-to-regex');
When importing the entire module, remember the '.js' suffix for ESM. CommonJS users must use the full package name.

This example demonstrates converting simple and complex glob patterns to regular expressions, using the matcher utility, and highlights key behaviors like extended glob support and implicit anchoring.

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)
Debug
Known issues
gotchaThe 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.
fix
Update import statements: `import { ... } from 'glob-to-regex.js';` or `require('glob-to-regex.js')`.
affects: >=1.0.1
gotchaThe `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.
fix
For negative pattern logic, combine `toMatcher` with your own conditional logic or use `toRegex` with custom RegExp for exclusions.
affects: >=1.0.0
gotchaGenerated 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`.
fix
Design glob patterns to match full paths (e.g., `**/*.js`) or wrap the generated regex in your own pattern if partial matching is desired, though this is generally not the library's intended use case.
affects: >=1.0.0
gotchaBrace 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.
fix
Avoid nesting brace groups in your glob patterns. Flatten complex alternations or use multiple `toRegex` calls if necessary.
affects: >=1.0.0
gotchaSupport 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.
fix
Pass `{ extglob: true }` as the second argument to `toRegex` or `toMatcher` to enable extended globbing.
affects: >=1.2.0
Errors
Common errors & fixes
Error: Cannot find module 'glob-to-regex'
Attempting to import the package using the old name without the '.js' suffix.
fix
Change the import path to 'glob-to-regex.js'.
TypeError: Invalid regular expression: /.../: Invalid character class
The glob pattern contains a malformed or unescaped character class that JavaScript's `RegExp` engine cannot parse.
fix
Ensure 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`.
The `toRegex` function produces anchored regular expressions (`^...$`), meaning `*.js` only matches files directly in the root, not in subdirectories.
fix
Use the `**` pattern for recursive matching, e.g., `**/*.js` to match files in any subdirectory.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies
tslibrequiredRequired for TypeScript helper functions, specified as a peer dependency.
Agent activity
2 hits · last 30 days
node
2
Resources