1.0.0 • Published 5 years ago

regex-combine-and v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

regex-combine-and

CircleCI NPM Downloads node License MIT

Combines multiple regexes into a single regex using lookaheads emulating AND behaviour

Highlights

  • Written in Typescript

  • Matches multiline strings

  • Matches only if all match phrases exists in test string irrepective of order

Usage

Combines multiple regexes into a single regex using lookaheads

Obviously, you can use the | (pipe?) to represent OR, but there is no AND operator in regex. But we can emulate an AND behaviour by using lookaheads.

This lets you match paragraphs of text that contain ALL of a certain phrase, but in no particular order

  const regexCombineAnd = require('regex-combine-and');

  combinedRegex = regexCombineAnd([/a/, /quick/, /brown/]); // /^(?=[\s\S]*a)/(?=[\s\S]*quick)(?=[\s\S]*brown)[\s\S]*$/m

  combinedRegex.test('a cat is quick but a brown fox is faster'); // true
  combinedRegex.test('a cat is \n quick but a \n brown \n fox is faster'); // true

  combinedRegex.test('a quick fox'); // false
  combinedRegex.test('a quick brown'); //true
  combinedRegex.test('a brown quick'); // true

License

MIT © Nivrith