string-lookup-manager
TypeScript icon, indicating that this package has built-in type declarations

4.0.2 • Public • Published

Package logo

Build Status npm Standard Shared Config

An easy way to collect regular expressions search results.

It you need to get an array of search results from several regular expressions. But search results may overlap and duplicate. This package will help you get clean results of this search.

Install

npm install string-lookup-manager

Usage

import LookupManager from 'string-lookup-manager';

const text = 'foo bar foobar bar foo';
const expressions = [/foo/gi, /bar/gi, /foobar/gi];
const manager = new LookupManager();
let match;

expressions.forEach(expression => {
  // Regex finds this:
  // { value: 'foo', start: 0 }
  // { value: 'foo', start: 8 }
  // { value: 'foo', start: 19 }
  // { value: 'bar', start: 4 }
  // { value: 'bar', start: 11 }
  // { value: 'bar', start: 15 }
  // { value: 'foobar', start: 8 }
  while ((match = expression.exec(text))) {
    console.log({ value: match[0], start: match.index });
    manager.add(match[0], match.index);
  }
});

// Lookup manager items:
//  LookupItem { value: 'foo', start: 0, end: 2 },
//  LookupItem { value: 'bar', start: 4, end: 6 },
//  LookupItem { value: 'foobar', start: 8, end: 13 },
//  LookupItem { value: 'bar', start: 15, end: 17 },
//  LookupItem { value: 'foo', start: 19, end: 21 }
console.log(manager.items);

Readme

Keywords

Package Sidebar

Install

npm i string-lookup-manager

Weekly Downloads

21

Version

4.0.2

License

MIT

Unpacked Size

12.7 kB

Total Files

12

Last publish

Collaborators

  • keindev