Blazing fast and accurate glob matcher written in JavaScript, with no dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions.
83
Low-level pattern parsing and regex compilation utilities for advanced use cases and custom implementations.
Parse a glob pattern to create the source string for a regular expression.
/**
* Parse a glob pattern to create source string for regex
* @param {string|string[]} pattern - Glob pattern(s) to parse
* @param {object} options - Configuration options
* @returns {object|object[]} Parsed pattern object(s) with tokens and metadata
*/
picomatch.parse(pattern, options);
interface ParseResult {
input: string;
tokens: Token[];
output: string;
regex: RegExp;
negated: boolean;
fastpaths: boolean;
// Additional parsing metadata
}
interface Token {
type: string;
value: string;
output?: string;
// Token-specific properties
}Usage Examples:
const picomatch = require('picomatch');
// Parse single pattern
const result = picomatch.parse('*.js');
console.log(result.input); // => '*.js'
console.log(result.output); // => '(?:(?!\.)(?=.)[^/]*?\.js)'
console.log(result.tokens); // => [{ type: 'star', value: '*' }, ...]
// Parse multiple patterns
const results = picomatch.parse(['*.js', '*.ts']);
console.log(results[0].output); // => parsed output for *.js
console.log(results[1].output); // => parsed output for *.ts
// Parse with options
const parsed = picomatch.parse('src/**/*.js', {
fastpaths: false,
strictSlashes: true
});
console.log(parsed.tokens); // => detailed token arrayScan a glob pattern to separate the pattern into segments and analyze its structure.
/**
* Scan glob pattern to separate into segments
* @param {string} input - Glob pattern to scan
* @param {object} options - Configuration options
* @returns {object} Scan result with pattern analysis
*/
picomatch.scan(input, options);
interface ScanResult {
prefix: string;
input: string;
start: number;
base: string;
glob: string;
isBrace: boolean;
isBracket: boolean;
isGlob: boolean;
isExtglob: boolean;
isGlobstar: boolean;
negated: boolean;
// Additional scan metadata
}Usage Examples:
// Scan basic pattern
const scan1 = picomatch.scan('*.js');
console.log(scan1.isGlob); // => true
console.log(scan1.base); // => ''
console.log(scan1.glob); // => '*.js'
// Scan complex pattern
const scan2 = picomatch.scan('!./src/**/*.js');
console.log(scan2.negated); // => true
console.log(scan2.prefix); // => '!./'
console.log(scan2.base); // => 'src'
console.log(scan2.glob); // => '**/*.js'
console.log(scan2.isGlobstar); // => true
// Scan brace pattern
const scan3 = picomatch.scan('*.{js,ts}');
console.log(scan3.isBrace); // => true
console.log(scan3.isGlob); // => true
// Scan extglob pattern
const scan4 = picomatch.scan('*.+(js|ts)');
console.log(scan4.isExtglob); // => true
// Scan with options
const scanResult = picomatch.scan('/path/to/*.js', {
parts: true
});Create a regular expression from a glob pattern.
/**
* Create regular expression from glob pattern
* @param {string} input - Glob pattern
* @param {object} options - Configuration options
* @param {boolean} returnOutput - Return output string instead of regex
* @param {boolean} returnState - Include state in result
* @returns {RegExp|string} Compiled regular expression or source string
*/
picomatch.makeRe(input, options, returnOutput, returnState);Usage Examples:
// Create regex
const regex1 = picomatch.makeRe('*.js');
console.log(regex1.test('file.js')); // => true
console.log(regex1.test('file.txt')); // => false
// Get regex source string
const source = picomatch.makeRe('*.js', {}, true);
console.log(source); // => '^(?:(?!\.)(?=.)[^/]*?\.js)$'
// With returnState option
const regexWithState = picomatch.makeRe('*.js', {}, false, true);
console.log(regexWithState.state); // => { /* parsing state */ }
// Complex patterns
const complexRegex = picomatch.makeRe('src/**/*.{js,ts}');
console.log(complexRegex.test('src/utils/helper.js')); // => true
console.log(complexRegex.test('src/deep/nested/app.ts')); // => true
// With options
const winRegex = picomatch.makeRe('src\\**\\*.js', { windows: true });
const nocaseRegex = picomatch.makeRe('*.JS', { nocase: true });
console.log(nocaseRegex.test('file.js')); // => trueCompile a regular expression from a parsed state object.
/**
* Compile regex from parsed state object
* @param {object} state - Parsed state from parse() method
* @param {object} options - Configuration options
* @param {boolean} returnOutput - Return output string instead of regex
* @param {boolean} returnState - Include state in result
* @returns {RegExp|string} Compiled regular expression or source string
*/
picomatch.compileRe(state, options, returnOutput, returnState);Usage Examples:
// Two-step compilation
const state = picomatch.parse('*.js');
const regex = picomatch.compileRe(state);
console.log(regex.test('file.js')); // => true
// Get compiled output string
const output = picomatch.compileRe(state, {}, true);
console.log(output); // => '^(?:(?!\.)(?=.)[^/]*?\.js)$'
// Compile with options
const compiledRegex = picomatch.compileRe(state, {
contains: true // Remove anchors
});
// Compile with state
const regexWithState = picomatch.compileRe(state, {}, false, true);
console.log(regexWithState.state === state); // => trueCreate a regular expression from a regex source string.
/**
* Create regex from source string
* @param {string} source - Regular expression source string
* @param {object} options - Configuration options
* @returns {RegExp} Regular expression object
*/
picomatch.toRegex(source, options);Usage Examples:
// Create regex from source
const regex1 = picomatch.toRegex('^(?:.*\\.js)$');
console.log(regex1.test('file.js')); // => true
// With flags
const regex2 = picomatch.toRegex('.*\\.js', { flags: 'i' });
console.log(regex2.test('FILE.JS')); // => true
// Case insensitive option
const regex3 = picomatch.toRegex('.*\\.JS', { nocase: true });
console.log(regex3.test('file.js')); // => true
// Error handling with debug
try {
const badRegex = picomatch.toRegex('[invalid regex', { debug: true });
} catch (error) {
console.log('Invalid regex pattern');
}
// Without debug (returns fallback regex)
const fallbackRegex = picomatch.toRegex('[invalid', { debug: false });
console.log(fallbackRegex.test('anything')); // => false (/$^/ fallback)Working with Tokens:
// Analyze pattern structure
const parsed = picomatch.parse('src/**/+(*.js|*.ts)');
parsed.tokens.forEach(token => {
console.log(`${token.type}: ${token.value}`);
});
// Output shows: text, globstar, text, extglob tokens
// Custom token processing
const customParsed = picomatch.parse('*.{js,ts,json}');
const braceTokens = customParsed.tokens.filter(t => t.type === 'brace');
console.log(braceTokens); // => brace expansion tokensPattern Optimization:
// Fast path detection
const fastPattern = picomatch.parse('*.js');
console.log(fastPattern.fastpaths); // => true (optimized)
const complexPattern = picomatch.parse('src/**/+(*.js|*.ts)');
console.log(complexPattern.fastpaths); // => false (complex parsing)
// Disable fast paths for consistent parsing
const slowParsed = picomatch.parse('*.js', { fastpaths: false });
console.log(slowParsed.tokens.length); // => more detailed tokensInstall with Tessl CLI
npx tessl i tessl/npm-picomatchevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10