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

2.0.1 • Public • Published

react-text-matcher

GitHub license npm version codecov build status

A React component that matches text using specified rules, supporting string and regular expressions. Developed based on text-matcher.

You can receive the matching results through render-props and have full control over how it renders. If you just want text highlighting, recommend using react-highlight-words, because it provides a simpler API.

Install

npm install @shhhplus/react-text-matcher --save

Usage

Basic

import TextMatcher from '@shhhplus/react-text-matcher';

const Demo = () => {
  return (
    <TextMatcher
      rules="everyone"
      text="Welcome everyone to come and join my birthday party."
    >
      {(nodes) => {
        return (
          <>
            {nodes.map((node, idx) => {
              return (
                <Fragment key={idx}>
                  {typeof node === 'string' ? node : <span>{node.text}</span>}
                </Fragment>
              );
            })}
          </>
        );
      }}
    </TextMatcher>
  );
};

RegExp

import TextMatcher from '@shhhplus/react-text-matcher';

const Demo = () => {
  return (
    <TextMatcher
      rules={[new RegExp('food', 'gi'), 'Apple']}
      text="AppleTodayFoodAppleHappySunFood"
    >
      {(nodes) => {
        return (
          <>
            {nodes.map((node, idx) => {
              return (
                <Fragment key={idx}>
                  {typeof node === 'string' ? node : <span>{node.text}</span>}
                </Fragment>
              );
            })}
          </>
        );
      }}
    </TextMatcher>
  );
};

Package Sidebar

Install

npm i @shhhplus/react-text-matcher

Weekly Downloads

1

Version

2.0.1

License

MIT

Unpacked Size

4.93 kB

Total Files

5

Last publish

Collaborators

  • shhhplus