Condense a set of words into a regular expression pattern
npm install strings-to-regexregexp
/(A(L|K|Z|R)|C(A|O|T)|DE|FL|GA|HI|I(D|L|N|A)|K(S|Y)|LA|M(E|D|A|I|N|S|O|T)|N(E|V|H|J|M|Y|C|D)|O(H|K|R)|PA|RI|S(C|D)|T(N|X)|UT|V(T|A)|W(A|V|I|Y))/
`
This library generates patterns like that to match a list of strings you provide.
_To reverse this process and list which strings a Regular Expression would match, try regex-to-strings._
Demo
API
$3
Generate a Regular Expression to match all strings in arrayOfStrings. Respects the casing of the strings. Returns a RegExp object.
`js
import { condense } from 'strings-to-regex';
const stringsToMatch = ['foo', 'foobar', 'Foo', 'fooBarBaz'];
const matcher = condense(stringsToMatch);
console.log(matcher); // /(foo(|bar|BarBaz)|Foo)/
`
---
$3
A variation of condense() that ignores the casing of the strings. Returns a RegExp object.
`js
import { condenseIgnoreCase } from 'strings-to-regex';
const stringsToMatch = ['foo', 'foobar', 'Foo', 'fooBarBaz'];
const matcher = condenseIgnoreCase(stringsToMatch);
console.log(matcher); // /foo(|bar(|baz))/i
``