What is wildcard-match?
The wildcard-match npm package is used for matching strings against wildcard patterns. It is useful for tasks such as filtering file paths, validating input strings, and more.
What are wildcard-match's main functionalities?
Basic Wildcard Matching
This feature allows you to check if a string matches a given wildcard pattern. In this example, 'foobar' matches the pattern 'foo*'.
const wildcard = require('wildcard-match');
const isMatch = wildcard('foo*')('foobar');
console.log(isMatch); // true
Case-Insensitive Matching
This feature allows for case-insensitive matching. In this example, 'FOOBAR' matches the pattern 'foo*' when case sensitivity is turned off.
const wildcard = require('wildcard-match');
const isMatch = wildcard('foo*', { caseSensitive: false })('FOOBAR');
console.log(isMatch); // true
Custom Wildcard Characters
This feature allows you to define custom wildcard characters. In this example, '#' is used as the wildcard character instead of the default '*'.
const wildcard = require('wildcard-match');
const isMatch = wildcard('foo#', { wildcard: '#' })('foo123');
console.log(isMatch); // true
Other packages similar to wildcard-match
minimatch
Minimatch is a powerful glob matching library that supports a wide range of glob patterns. It is more feature-rich compared to wildcard-match, offering advanced pattern matching capabilities.
micromatch
Micromatch is a fast and lightweight glob matcher that supports extended glob patterns and advanced matching features. It is known for its performance and flexibility, making it a good alternative to wildcard-match.
multimatch
Multimatch is a library that allows you to match multiple glob patterns against a list of strings. It is built on top of minimatch and provides additional functionality for handling multiple patterns efficiently.
wildcard-match
Compile a glob-like pattern into a regular expression.
import wcm from 'wildcard-match'
const regExp = wcm('wildc?rd-mat*')
regExp.test('wildcard-match')
?
matches a single arbitrary character*
matches zero or more arbitrary characters
When a separator such as /
is provided, the above wildcards will only match non-separator characters, and the following is activated:
**
matches any number of segments when used as a whole segment (i.e. /**/
in the middle, **/
at the beginning or /**
at the end of a separated string)
wcm('src/**/*.?s', '/').test('src/lib/component/index.js')
Install
npm install wildcard-match
Usage
wildcardMatch(pattern, separator?): RegExp
The default export is a function that takes a string or an array of strings and an optional
separator (or an options object with a separator property). It compiles the pattern into
a RegExp object that can be used to match strings with the pattern.
import wcm from 'wildcard-match'
const regExp = wcm('foo*/b?r', '/')
regExp.test('foo/bar')
regExp.test('foobar')
const regExp = wcm(['one.*', '*.two'], { separator: '.' })
regExp.test('one.two')
regExp.test('one.three')
regExp.test('three.two')
regExp.test('one')
regExp.test('two')
regExp.test('one.two.three')
regExp.test('three.false')
The returned RegExp has pattern
and options
properties set to the original values.
const regExp = wcm('p?tt?rn', '/')
match.pattern
match.options
A pattern can have ?
and *
escaped with a backslash so that they are treated as literal characters and not wildcards.
const regExp = wcm('foo\\*')
regExp.test('foo')
regExp.test('foobar')
regExp.test('foo*')
When no separator is given, **
acts as *
.
const regExp = wcm('foo/**bar')
regExp.test('foo/bar')
regExp.test('foo/bazbar')
regExp.test('foo/baz/qux/bar')
Examples
import wcm from 'wildcard-match'
const regExp = wcm('*?.js')
regExp.test('index.js')
regExp.test('src/index.js')
regExp.test('.js')
regExp.test('src')
import wcm from 'wildcard-match'
const regExp = wcm('src/**/index.?s', '/')
regExp.test('src/index.js')
regExp.test('src/lib/index.ts')
regExp.test('src/lib/component/test/index.ts')
regExp.test('src')
regExp.test('index.js')
regExp.test('src/index.js/lib')
import wcm from 'wildcard-match'
const regExp = wcm('**.*.example.com', '.')
regExp.test('example.com')
regExp.test('foo.example.com')
regExp.test('foo.bar.example.com')
regExp.test('foo.bar.baz.qux.example.com')
regExp.test('foo.example.com.bar')
Related
License
ISC