or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-glob-to-regexp

Convert globs to regular expressions

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/glob-to-regexp@0.4.x

To install, run

npx @tessl/cli install tessl/npm-glob-to-regexp@0.4.0

index.mddocs/

Glob to RegExp

Glob 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.

Package Information

  • Package Name: glob-to-regexp
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install glob-to-regexp

Core Imports

const globToRegExp = require('glob-to-regexp');

For ES modules (using dynamic import):

const { default: globToRegExp } = await import('glob-to-regexp');

Basic Usage

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");      // true

Capabilities

Glob Conversion

Converts 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:

  • glob (string, required): The glob pattern to convert to a regular expression. Must be a string or TypeError is thrown.
  • opts (object, optional): Configuration options for the conversion
    • extended (boolean, default: false): Enable bash-like extended glob features
    • globstar (boolean, default: false): Enable globstar pattern behavior
    • flags (string, default: ""): RegExp flags to pass to the RegExp constructor

Returns: RegExp object that matches the glob pattern

Error Handling: Throws TypeError with message "Expected a string" when glob parameter is not a string.

Pattern Support

Basic Patterns

  • * - Matches zero or more characters
  • Literal characters match themselves
  • Special regex characters are automatically escaped: /, $, ^, +, ., (, ), =, !, |
const re = globToRegExp("f*uck");
re.test("firetruck");  // true
re.test("fuck");       // true
re.test("pluck");      // false

Extended Patterns (extended: true)

  • ? - 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");  // false

Globstar Patterns (globstar: true)

Changes 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");  // true

RegExp Flags

The flags option controls RegExp behavior, particularly anchoring:

  • Without 'g' flag: Pattern is anchored with ^ and $ (matches entire string)
  • With 'g' flag: Pattern matches anywhere in the string (no anchors added)
// 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");  // true

Complete Usage Examples

const 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