@shhhplus/text-matcher
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

text-matcher

GitHub license npm version codecov build status

This tool can use specified rules to match text. If you want to use it in React, you can consider choosing @shhhplus/react-text-matcher.

Install

npm install @shhhplus/text-matcher --save

Usage

Basic

import createTextMatcher from '@shhhplus/text-matcher';

const rules = ['everyone', 'birthday'];
const tm = createTextMatcher(rules);
const result = tm.exec('Welcome everyone to come and join my birthday party.');

// result:
[
  'Welcome ',
  {
    text: 'everyone',
    payload: { position: 8, ruleIndex: 0, matchIndex: 0 },
  },
  ' to come and join my ',
  {
    text: 'birthday',
    payload: { position: 37, ruleIndex: 1, matchIndex: 1 },
  },
  ' party.',
];

RegExp

import createTextMatcher from '@shhhplus/text-matcher';

const rules = [new RegExp('apple', 'gi'), new RegExp('food')];
const tm = createTextMatcher(rules);
const result = tm.exec('apple_sun_banana_Apple_sun_food_sun');

// result:
[
  {
    text: 'apple',
    payload: { position: 0, ruleIndex: 0, matchIndex: 0 },
  },
  '_sun_banana_',
  {
    text: 'Apple',
    payload: { position: 17, ruleIndex: 0, matchIndex: 1 },
  },
  '_sun_',
  {
    text: 'food',
    payload: { position: 27, ruleIndex: 1, matchIndex: 2 },
  },
  '_sun',
];

Parameters

name type
rules string | RegExp | Array<string | RegExp>

Package Sidebar

Install

npm i @shhhplus/text-matcher

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

5.74 kB

Total Files

5

Last publish

Collaborators

  • shhhplus