regex-combine-and
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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

Package Sidebar

Install

npm i regex-combine-and

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

175 kB

Total Files

10

Last publish

Collaborators

  • nivrith