regex-combine-and

Combines multiple regexes into a single regex using lookaheads

Usage no npm install needed!

<script type="module">
  import regexCombineAnd from 'https://cdn.skypack.dev/regex-combine-and';
</script>

README

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