README
wildcard-regex
Super simple wildcard tools for generating string patterns and RegExp objects
Installation
To install wildcard-regex
to your project, run:
npm install --save wildcard-regex
Getting Started
wildcard-regex
is a simple set of tools that can convert raw wildcard
patterns (strings or array of strings with wildcards denoted as *
) to
pattern strings or RegExp objects.
Using wildcard-regex
to create a RegExp object for pattern matching is as
simple as requiring it as a variable in your script and giving it a raw
wildcard pattern.
ES6/ES2015 Syntax
import { wildcardRegExp } from 'wildcard-regex';
const regex = wildcardRegExp('Begin the sentence*and end it here');
// This generates the following RegExp object:
// /Begin the sentence.*and end it here/
regex.test('Begin the sentence, then add this portion and end it here');
// true
Older ES5 Syntax
var wildcard = require('wildcard-regex');
var regex = wildcard.wildcardRegExp('Begin the sentence*and end it here');
// This generates the following RegExp object:
// /Begin the sentence.*and end it here/
regex.test('Begin the sentence, then add this portion and end it here');
// true
You can also test for an array of wildcard patterns by passing them into
wildcard-regex
as an Array
of Strings
, as such:
ES6/ES2015 Syntax
import { wildcardRegExp } from 'wildcard-regex';
const regex = wildcardRegExp(['Test*This'], ['Or*This']);
Older ES5 Syntax
var wildcard= require('wildcard-regex');
var regex = wildcard.wildcardRegExp(['Test*This'], ['Or*This']);
If you want to store your wildcard pattern in a manner that does not work with RegExp objects, you might want to create a pattern string to represent the regex pattern instead of a full RegExp object.
This is easy to do by using the wildcardPattern
method, which returns a
string pattern instead of a RegExp object. Here's an example:
ES6/ES2015 Syntax
import { wildcardPattern } from 'wildcard-regex';
const pattern = wildcardPattern('*kevinzwhuang/wildcard-regex')
// This creates a string ready for storage and for conversion to a RegExp
// object later.
// '^.*kevinzwhuang/wildcard-regex