@shhhplus/text-matcher
Version:
This tool can use specified rules to match text.
74 lines (58 loc) • 2.09 kB
Markdown
[](https://github.com/shhhplus/text-matcher/blob/main/LICENSE) [](https://www.npmjs.com/package/@shhhplus/text-matcher) [](https://codecov.io/gh/shhhplus/text-matcher) [](https://github.com/shhhplus/text-matcher/actions)
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](https://www.npmjs.com/package/@shhhplus/react-text-matcher).
```sh
npm install @shhhplus/text-matcher --save
```
```js
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.',
];
```
```js
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',
];
```
| name | type |
| ----- | ------------------------------------------- |
| rules | string \| RegExp \| Array<string \| RegExp> |