https://github.com/gruhn/regex-utils
TypeScript library for regex intersection, complement and other utilities that go beyond string matching.
https://github.com/gruhn/regex-utils
javascript regex regexp regular-expression regular-expressions typescript
Last synced: about 2 months ago
JSON representation
TypeScript library for regex intersection, complement and other utilities that go beyond string matching.
- Host: GitHub
- URL: https://github.com/gruhn/regex-utils
- Owner: gruhn
- Created: 2025-01-19T10:28:22.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-05-28T21:50:41.000Z (about 1 year ago)
- Last Synced: 2025-06-16T10:58:23.315Z (about 1 year ago)
- Topics: javascript, regex, regexp, regular-expression, regular-expressions, typescript
- Language: TypeScript
- Homepage: https://gruhn.github.io/regex-utils/
- Size: 296 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-regex - regex-utils - Check regex equivalence, build regex intersections, and other utilities. (JavaScript regex libraries / Regex processors, utilities, and more)
README
# Regex Utils
Zero-dependency TypeScript library for regex utilities that go beyond string matching.
These are surprisingly hard to come by for any programming language. ✨
- [Documentation](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html)
- Online demos:
- [RegExp Equivalence Checker](https://gruhn.github.io/regex-utils/equiv-checker.html)
- [Random Password Generator](https://gruhn.github.io/regex-utils/password-generator.html?constraints=%5E%5B%5Cx21-%5Cx7E%5D%7B16%7D%24%0A%5BA-Z%5D%0A%5Ba-z%5D%0A%5B0-9%5D)
## API Overview 🚀
- 🔗 Set-style operations:
- [.and(...)](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#and) - Compute intersection of two regex.
- [.not()](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#not) - Compute the complement of a regex.
- [.without(...)](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#without) - Compute the difference of two regex.
- ✅ Set-style predicates:
- [.isEquivalent(...)](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#isEquivalent) - Check whether two regex match the same strings.
- [.isSubsetOf(...)](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#isSubsetOf)
- [.isSupersetOf(...)](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#isSupersetOf)
- [.isDisjointFrom(...)](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#isDisjointFrom)
- [.isEmpty()](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#isEmpty) - Check whether a regex matches no strings.
- 📜 Generate strings:
- [.sample(...)](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#sample) - Generate random strings matching a regex.
- [.enumerate()](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#enumerate) - Exhaustively enumerate strings matching a regex.
- 🔧 Miscellaneous:
- [.size()](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#size) - Count the number of strings that a regex matches.
- [.derivative(...)](https://gruhn.github.io/regex-utils/interfaces/RegexBuilder.html#derivative) - Compute a Brzozowski derivative of a regex.
- and others...
## Installation 📦
```bash
npm install @gruhn/regex-utils
```
```typescript
import { RB } from '@gruhn/regex-utils'
```
## Syntax Support
| Feature | Support | Examples |
|---------|---------|-------------|
| Quantifiers | ✅ | `a*`, `a+`, `a{3,10}`, `a?` |
| Lazy Quantifiers | ✅ | `a*?`, `a+?`, `a{3,10}?`, `a??` |
| Alternation | ✅ | `a\|b` |
| Character classes | ✅ | `.`, `\w`, `[a-zA-Z]`, ... |
| Escaping | ✅ | `\$`, `\.`, ... |
| (Non-)capturing groups | ✅ | `(?:...)`, `(...)` |
| Start/end anchors | ⚠️1 | `^`, `$` |
| Lookahead | ⚠️2 | `(?=...)`, `(?!...)` |
| Lookbehind | ⚠️2 | `(?<=...)`, `(?
```
A straightforward attempt would be:
```typescript
```
The problem is that `.*` also matches the end marker `-->`,
so this is also a match:
```typescript
and this shouldn't be part of it -->
```
We need to specify that the inner part can be any string that does not contain `-->`.
With `.not()` (aka. regex complement) this is easy:
```typescript
import { RB } from '@gruhn/regex-utils'
const commentStart = RB('.*$/).not()
const commentEnd = RB('-->')
const comment = commentStart.concat(commentInner).concat(commentEnd)
```
With `.toRegExp()` we can convert back to a native JavaScript regex:
```typescript
comment.toRegExp()
```
```
/^