glob-to-regexp
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Glob-to-regexp is a potent tool with which you can convert wildcard-style globs into regular expressions. It turns a format like "*.min.js"
into a regular expression like /^.*\.min\.js$/
. The package also supports bash-like globs such as ?
for single-character matches, [a-z]
for character ranges, and {*.html, *.js}
for multiple alternatives, offering one a great deal of flexibility in their search pattern.
The use of glob-to-regexp is fairly straightforward. First, you need to install it using npm by running npm install glob-to-regexp
. Then, in your JavaScript code, require the module like this: var globToRegExp = require('glob-to-regexp');
. Once you have the globToRegExp
function available, you can call it with a glob string to get a regular expression. For example,
var re = globToRegExp("p*uck");
You can then use this regular expression with the .test()
method on any string to check if it matches the pattern. For example,
re.test("pluck"); // returns true
One thing to remember is that if you want to use extended globs, you need to call globToRegExp
with the second parameter as { extended: true }
. For instance,
var re = globToRegExp("*/www/{*.js,*.html}", { extended: true });
re.test("http://example.com/www/app.js"); // returns true
The official documentation for glob-to-regexp is not in a separate location but rather embedded in its GitHub page. You can find all the information you need in the README file at the GitHub repository located at https://github.com/fitzgen/glob-to-regexp. In case of any updates or changes, this is the first place where any modifications will be reflected. The README file is very comprehensive, providing installation and usage instructions as well as examples of how to effectively employ glob-to-regexp.