regex-combine-and
v1.0.0
Published
Combines multiple regexes into a single regex using lookaheads
Downloads
2
Maintainers
Readme
regex-combine-and
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