Skip to content

globToRegExp

Compiles a simple glob pattern into a RegExp that matches the whole string: * matches any sequence of UTF-16 code units (including none), ? matches exactly one UTF-16 code unit. Everything else is matched literally.

This is not a full shell/minimatch glob — no brace expansion, character classes, or path-separator-aware **, just the two textbook wildcards. Like the rest of this library, lengths are counted in UTF-16 code units: ? matches one code unit, not one Unicode code point, so it matches only half of a surrogate pair (e.g. an emoji outside the Basic Multilingual Plane).

Available since v3.1.1

import { globToRegExp } from '@helpers4/string';
// or, from the all-in-one package (same code, one install):
import { globToRegExp } from 'helpers4/string';
globToRegExp(pattern: string, caseSensitive: boolean): RegExp
ParameterTypeDescription
patternstringThe glob pattern to compile
caseSensitivebooleanWhether the resulting `RegExp` is case-sensitive. Defaults to `true`.

RegExp — A RegExp matching strings that satisfy pattern in full

”*” matches any sequence of characters.

globToRegExp('*.test.ts').test('helper.test.ts')
// => true

Match a fixed-width placeholder with ”?”

Section titled “Match a fixed-width placeholder with ”?””

”?” matches exactly one character — handy for fixed-width codes.

globToRegExp('report-????.csv').test('report-2026.csv')
// => true

Reuse the same pattern for many strings efficiently

Section titled “Reuse the same pattern for many strings efficiently”

Compile the RegExp once and reuse it for every check instead of recompiling per candidate. If you also need to cache across many different call sites, wrap globToRegExp itself with memoize() from @helpers4/function.

const isTestFile = globToRegExp('*.test.ts');
['a.test.ts', 'a.spec.ts'].filter(name => isTestFile.test(name))
// => ['a.test.ts']

View source on GitHub