@parameter1/build-match-regex
v2.0.0
Published
Builds regular expressions for matching parts of phrases to values.
Downloads
129
Readme
Build Match Regex
Builds regular expressions for matching parts of a phrase to a value. Useful for autocompleters, typeaheads, or quasi-search functions.
Installation
yarn add @parameter1/build-match-regex
Usage
import buildMatchRegex from '@parameter1/build-match-regex';
const phrase = 'quick brown fox';
const regex = buildMatchRegex({
phrase,
// can be one of 'starts', 'ends', 'exact', or 'contains'
position: 'contains', // default
// can be either 'all' or 'any'
words: 'all', // default
insensitive: true, // default, set to false to force case-sensitivity
});
regex.test('brown fox'); // false
regex.test('brown fox quick'); // true
regex.test('Quickest Brownest Fox!'); // true
Options
Assuming a phrase of quick brown fox
...
Position starts
:
- Words:
any
- Where a value starts with
quick
,brown
, orfox
/^quick|^brown|^fox/i
- Where a value starts with
- Words:
all
- Where a value literally starts with
quick brown fox
/^quick brown fox/i
- Where a value literally starts with
Position ends
:
- Words:
any
- Where a value ends with
quick
,brown
, orfox
/quick$|brown$|fox$/i
- Where a value ends with
- Words:
all
- Where a value literally ends with
quick brown fox
/quick brown fox$/i
- Where a value literally ends with
Position: exact
:
- Words:
any
- Where a value exactly matches
quick
,brown
, orfox
/^quick$|^brown$|^fox$/i
- Where a value exactly matches
- Words:
all
- Where a value exactly matches
quick brown fox
/^quick brown fox$/i
- Where a value exactly matches
Position contains
:
- Words:
any
- Where a value contains any partial matches of
quick
,brown
, orfox
/quick|brown|fox/i
- Where a value contains any partial matches of
- Words:
all
- Where a value contains all partial matches of
quick
,brown
, andfox
/(?=.*quick)(?=.*brown)(?=.*fox)/i
- Where a value contains all partial matches of