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.jsVerified import paths — ran on the pinned version, not inferred.
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.
Update import statements: `import { ... } from 'glob-to-regex.js';` or `require('glob-to-regex.js')`.For negative pattern logic, combine `toMatcher` with your own conditional logic or use `toRegex` with custom RegExp for exclusions.
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.
Avoid nesting brace groups in your glob patterns. Flatten complex alternations or use multiple `toRegex` calls if necessary.
Pass `{ extglob: true }` as the second argument to `toRegex` or `toMatcher` to enable extended globbing.Change the import path to 'glob-to-regex.js'.
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.
Use the `**` pattern for recursive matching, e.g., `**/*.js` to match files in any subdirectory.