{"id":10953,"library":"glob-to-regex.js","title":"Glob to Regex","description":"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.","status":"active","version":"1.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/streamich/glob-to-regex","tags":["javascript","glob","regex","regexp","pattern","matcher","path","filesystem","wildcard","typescript"],"install":[{"cmd":"npm install glob-to-regex.js","lang":"bash","label":"npm"},{"cmd":"yarn add glob-to-regex.js","lang":"bash","label":"yarn"},{"cmd":"pnpm add glob-to-regex.js","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for TypeScript helper functions, specified as a peer dependency.","package":"tslib","optional":false}],"imports":[{"note":"The package name in import paths requires the '.js' suffix for ESM. CommonJS usage needs `.toRegex` property access.","wrong":"import { toRegex } from 'glob-to-regex';\nconst toRegex = require('glob-to-regex.js').toRegex;","symbol":"toRegex","correct":"import { toRegex } from 'glob-to-regex.js';"},{"note":"The package name in import paths requires the '.js' suffix for ESM. CommonJS usage needs `.toMatcher` property access.","wrong":"import { toMatcher } from 'glob-to-regex';\nconst toMatcher = require('glob-to-regex.js').toMatcher;","symbol":"toMatcher","correct":"import { toMatcher } from 'glob-to-regex.js';"},{"note":"When importing the entire module, remember the '.js' suffix for ESM. CommonJS users must use the full package name.","wrong":"const GlobToRegex = require('glob-to-regex');","symbol":"glob-to-regex.js (module)","correct":"import * as GlobToRegex from 'glob-to-regex.js';"}],"quickstart":{"code":"import {toRegex, toMatcher} from 'glob-to-regex.js';\n\n// Build a RegExp from a glob\nconst re = toRegex('src/**/test.ts');\nconsole.log(`'src/a/b/test.ts' matches 'src/**/test.ts': ${re.test('src/a/b/test.ts')}`); // true\nconsole.log(`'src/test.ts' matches 'src/**/test.ts': ${re.test('src/test.ts')}`);     // true\nconsole.log(`'src/test.tsx' matches 'src/**/test.ts': ${re.test('src/test.tsx')}`);    // false\n\n// Build a predicate function from a pattern or an array of patterns\nconst match = toMatcher(['**/*.ts', '!**/*.d.ts']); // negative patterns are not special; use a RegExp if needed\nconsole.log(`'index.ts' matches ['**/*.ts', '!**/*.d.ts']: ${match('index.ts')}`);    // true\nconsole.log(`'types.d.ts' matches ['**/*.ts', '!**/*.d.ts']: ${match('types.d.ts')}`);  // true (negation is not parsed specially)\n\n// Demonstrate extended globs (requires option)\nconst reExt = toRegex('file.@(jpg|png|gif)', {extglob: true});\nconsole.log(`'file.jpg' matches 'file.@(jpg|png|gif)' with extglob: ${reExt.test('file.jpg')}`); // true\nconsole.log(`'file.txt' matches 'file.@(jpg|png|gif)' with extglob: ${reExt.test('file.txt')}`); // false\n\n// Demonstrate anchored regex behavior\nconst reAnchored = toRegex('*.js');\nconsole.log(`'my-file.js' matches '*.js': ${reAnchored.test('my-file.js')}`); // true\nconsole.log(`'path/to/my-file.js' matches '*.js': ${reAnchored.test('path/to/my-file.js')}`); // false (due to implicit anchoring) ","lang":"typescript","description":"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."},"warnings":[{"fix":"Update import statements: `import { ... } from 'glob-to-regex.js';` or `require('glob-to-regex.js')`.","message":"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.","severity":"gotcha","affected_versions":">=1.0.1"},{"fix":"For negative pattern logic, combine `toMatcher` with your own conditional logic or use `toRegex` with custom RegExp for exclusions.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Avoid nesting brace groups in your glob patterns. Flatten complex alternations or use multiple `toRegex` calls if necessary.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Pass `{ extglob: true }` as the second argument to `toRegex` or `toMatcher` to enable extended globbing.","message":"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.","severity":"gotcha","affected_versions":">=1.2.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change the import path to 'glob-to-regex.js'.","cause":"Attempting to import the package using the old name without the '.js' suffix.","error":"Error: Cannot find module 'glob-to-regex'"},{"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.","cause":"The glob pattern contains a malformed or unescaped character class that JavaScript's `RegExp` engine cannot parse.","error":"TypeError: Invalid regular expression: /.../: Invalid character class"},{"fix":"Use the `**` pattern for recursive matching, e.g., `**/*.js` to match files in any subdirectory.","cause":"The `toRegex` function produces anchored regular expressions (`^...$`), meaning `*.js` only matches files directly in the root, not in subdirectories.","error":"My glob pattern `*.js` isn't matching `src/file.js`."}],"ecosystem":"npm"}