Convert globs to regular expressions
npx @tessl/cli install tessl/npm-glob-to-regexp@0.4.0Glob to RegExp converts glob patterns (wildcard-style patterns like *.js or **/*.html) into JavaScript regular expressions. It supports standard glob syntax with extended features like character ranges, alternative patterns, and globstar for recursive directory matching.
npm install glob-to-regexpconst globToRegExp = require('glob-to-regexp');For ES modules (using dynamic import):
const { default: globToRegExp } = await import('glob-to-regexp');const globToRegExp = require('glob-to-regexp');
// Basic glob patterns
const re = globToRegExp("*.js");
re.test("app.js"); // true
re.test("style.css"); // false
// Match file paths
const pathRe = globToRegExp("src/**/*.js");
pathRe.test("src/components/Header.js"); // true
pathRe.test("src/utils/helpers.js"); // true
// Extended globs with alternatives
const extRe = globToRegExp("{*.html,*.js}", { extended: true });
extRe.test("index.html"); // true
extRe.test("app.js"); // trueConverts a glob pattern string to a JavaScript RegExp object with configurable behavior.
/**
* Convert a glob pattern to a regular expression
* @param {string} glob - The glob pattern to convert
* @param {object} [opts] - Configuration options
* @param {boolean} [opts.extended=false] - Enable extended glob features (?, [], {})
* @param {boolean} [opts.globstar=false] - Enable globstar (**) behavior
* @param {string} [opts.flags=""] - RegExp flags to apply
* @returns {RegExp} A regular expression that matches the glob pattern
* @throws {TypeError} When glob is not a string
*/
function globToRegExp(glob, opts)Parameters:
Returns: RegExp object that matches the glob pattern
Error Handling: Throws TypeError with message "Expected a string" when glob parameter is not a string.
* - Matches zero or more characters/, $, ^, +, ., (, ), =, !, |const re = globToRegExp("f*uck");
re.test("firetruck"); // true
re.test("fuck"); // true
re.test("pluck"); // false? - Matches exactly one character[abc] - Character sets and ranges{alt1,alt2} - Alternative patterns (choice groups)// Single character matching
const singleRe = globToRegExp("f?o", { extended: true });
singleRe.test("foo"); // true
singleRe.test("fooo"); // false
// Character ranges
const rangeRe = globToRegExp("fo[oz]", { extended: true });
rangeRe.test("foo"); // true
rangeRe.test("foz"); // true
rangeRe.test("fog"); // false
// Alternative patterns
const altRe = globToRegExp("foo{bar,baaz}", { extended: true });
altRe.test("foobar"); // true
altRe.test("foobaaz"); // true
altRe.test("foobuzz"); // falseChanges the behavior of * and enables ** for recursive directory matching:
* - Matches characters within a single path segment (excludes /)** - Matches zero or more path segments when surrounded by / or at boundaries// Without globstar (default behavior)
const defaultRe = globToRegExp("http://foo.com/*");
defaultRe.test("http://foo.com/bar/baz/file.js"); // true
// With globstar
const globstarRe = globToRegExp("http://foo.com/*", { globstar: true });
globstarRe.test("http://foo.com/bar/baz/file.js"); // false (crosses path segments)
globstarRe.test("http://foo.com/file.js"); // true
// Using ** for recursive matching
const recursiveRe = globToRegExp("http://foo.com/**", { globstar: true });
recursiveRe.test("http://foo.com/bar/baz/file.js"); // trueThe flags option controls RegExp behavior, particularly anchoring:
^ and $ (matches entire string)// Anchored matching (default)
const anchoredRe = globToRegExp("min");
anchoredRe.test("jquery.min.js"); // false (doesn't match entire string)
// Global matching
const globalRe = globToRegExp("min.js", { flags: "g" });
globalRe.test("jquery.min.js"); // true (matches anywhere)
// Case insensitive
const caseRe = globToRegExp("*.JS", { flags: "i" });
caseRe.test("app.js"); // trueconst globToRegExp = require('glob-to-regexp');
// File extension matching
const jsRe = globToRegExp("*.js");
console.log(jsRe.test("app.js")); // true
console.log(jsRe.test("app.js.map")); // false
// Path matching with globstar
const pathRe = globToRegExp("/foo/**/*.txt", { globstar: true });
console.log(pathRe.test("/foo/bar.txt")); // true
console.log(pathRe.test("/foo/bar/baz.txt")); // true
console.log(pathRe.test("/foo/bar/baz/qux.txt")); // true
// Complex extended patterns
const complexRe = globToRegExp("http://?o[oz].b*z.com/{*.js,*.html}", {
extended: true
});
console.log(complexRe.test("http://foo.baaz.com/jquery.min.js")); // true
console.log(complexRe.test("http://moz.buzz.com/index.html")); // true
// Global flag for substring matching
const substringRe = globToRegExp("jquery", { flags: "g" });
console.log(substringRe.test("lib/jquery.min.js")); // true