{"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,"search_vec":"'1.2.0':11 '2025':40 'activ':32 'altern':115 'anchor':118 'behavior':137 'bundl':151 'cadenc':45 'charact':97 'class':98,105 'comprehens':81 'concern':155 'consist':43 'convert':19 'crucial':125 'current':7 'demonstr':41 'develop':44,111 'differenti':47 'direct':67 'enabl':90 'engin':59 'ensur':108 'environ':149 'experi':112 'explicit':117 'express':122 'extend':88 'featur':36,86 'filesystem':163 'first':104 'first-class':103 'focus':50 'footprint':144 'full':129 'full-path':128 'function':69,79 'generat':72,120 'glob':1,23,85,89,157 'glob-to-regex.js':5 'includ':48,87 'javascript':27,156 'key':46 'late':39 'leverag':54 'librari':6 'lightweight':14 'maintain':33 'make':145 'match':61,131 'matcher':161 'nativ':26 'new':140 'nuanc':138 'object':29 'optim':57 'option':93 'packag':100 'path':130,162 'pattern':24,160 'perform':16,52 'precis':127 'predic':78 'provid':12 'provis':63 'recent':35 'regex':3,58,158 'regexp':28,71,159 'regular':121 'releas':37 'robust':110 'shell':21 'shell-styl':20 'ship':101 'sinc':94 'size':152 'small':143 'solut':17 'stabl':8 'style':22 'suitabl':147 'support':82 'though':132 'tomatch':75 'toregex':68 'type':107 'typescript':106,165 'unlik':113 'user':141 'util':76 'v1.2.0':95 'v8':55 'various':84 'version':10 'via':91 'wildcard':164","created_at":"2026-04-19T13:35:29.580175+00:00","updated_at":"2026-04-19T13:35:29.580175+00:00","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","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/streamich/glob-to-regex","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/glob-to-regex.js","openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}