Generate a regex from a string or array of strings.
to-regex
is tiny module that helps us to easily create R.E from a string
or an array
of strings.
Get it: npm install to-regex
Sample usage:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| const toRegex = require('to-regex');
console.log(toRegex('foo'));
//=> /^(?:foo)$/
console.log(toRegex('foo', {negate: true}));
//=> /^(?:(?:(?!^(?:foo)$).)*)$/
console.log(toRegex('foo', {contains: true}));
//=> /(?:foo)/
console.log(toRegex(['foo', 'bar'], {negate: true}));
//=> /^(?:(?:(?!^(?:(?:foo)|(?:bar))$).)*)$/
console.log(toRegex(['foo', 'bar'], {negate: true, contains: true}));
//=> /^(?:(?:(?!(?:(?:foo)|(?:bar))).)*)$/
|
GIF FTW!