|
1 | 1 | # Regex Utils |
2 | 2 |
|
3 | 3 | Zero-dependency TypeScript library for regex intersection, complement and other utilities that go beyond string matching. |
4 | | -These are surprisingly hard to come by for any programming language: |
5 | | - |
6 | | - - [`intersection`](https://gruhn.github.io/regex-utils/functions/High-level_API.intersection.html): |
7 | | - Combines multiple `RegExp` into a single `RegExp` that descibes their intersection. |
8 | | - - [`complement`](https://gruhn.github.io/regex-utils/functions/High-level_API.complement.html): |
9 | | - Returns a `RegExp` describes the opposite of the input `RegExp`. |
10 | | - - [`size`](https://gruhn.github.io/regex-utils/functions/High-level_API.size.html): |
11 | | - Returns the number of strings matching the input `RegExp`. |
12 | | - - [`enumerate`](https://gruhn.github.io/regex-utils/functions/High-level_API.enumerate.html): |
13 | | - Returns a stream of strings matching the input `RegExp`. |
14 | | - - [`derivative`](https://gruhn.github.io/regex-utils/functions/High-level_API.derivative.html): |
15 | | - Computes a Brzozowski derivative of the input `RegExp`. |
| 4 | +These are surprisingly hard to come by for any programming language. |
| 5 | + |
| 6 | +```typescript |
| 7 | +import { intersection, size, enumerate } from '@gruhn/regex-utils' |
| 8 | + |
| 9 | +// `intersection` combines multiple regex into one: |
| 10 | +const passwordRegex = intersection( |
| 11 | + /^[a-zA-Z0-9]{12,32}$/, // 12-32 alphanumeric characters |
| 12 | + /[0-9]/, // at least one number |
| 13 | + /[A-Z]/, // at least one upper case letter |
| 14 | + /[a-z]/, // at least one lower case letter |
| 15 | +) |
| 16 | + |
| 17 | +// `size` to calculates the number of strings matching the regex: |
| 18 | +console.log(size(passwordRegex)) |
| 19 | +// 2301586451429392354821768871006991487961066695735482449920n |
| 20 | + |
| 21 | +// `enumerate` returns a stream of strings matching the regex: |
| 22 | +for (const sample of enumerate(passwordRegex).take(10)) { |
| 23 | + console.log(sample) |
| 24 | +} |
| 25 | +// aaaaaaaaaaA0 |
| 26 | +// aaaaaaaaaa0A |
| 27 | +// aaaaaaaaaAA0 |
| 28 | +// aaaaaaaaaA00 |
| 29 | +// aaaaaaaaaaA1 |
| 30 | +// aaaaaaaaa00A |
| 31 | +// baaaaaaaaaA0 |
| 32 | +// AAAAAAAAAA0a |
| 33 | +// aaaaaaaaaAA1 |
| 34 | +// aaaaaaaaaa0B |
| 35 | +``` |
16 | 36 |
|
17 | 37 | ## Installation |
18 | 38 |
|
|
0 commit comments