2.0.0 • Published 2 years ago

@parameter1/build-match-regex v2.0.0

Weekly downloads
8
License
MIT
Repository
github
Last release
2 years ago

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, or fox
    • /^quick|^brown|^fox/i
  • Words: all
    • Where a value literally starts with quick brown fox
    • /^quick brown fox/i

Position ends:

  • Words: any
    • Where a value ends with quick, brown, or fox
    • /quick$|brown$|fox$/i
  • Words: all
    • Where a value literally ends with quick brown fox
    • /quick brown fox$/i

Position: exact:

  • Words: any
    • Where a value exactly matches quick, brown, or fox
    • /^quick$|^brown$|^fox$/i
  • Words: all
    • Where a value exactly matches quick brown fox
    • /^quick brown fox$/i

Position contains:

  • Words: any
    • Where a value contains any partial matches of quick, brown, or fox
    • /quick|brown|fox/i
  • Words: all
    • Where a value contains all partial matches of quick, brown, and fox
    • /(?=.*quick)(?=.*brown)(?=.*fox)/i
2.0.0

2 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.9

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago