zeroreg
v0.1.2
Published
Regex was a mistake. Just like your ex. A human-readable regex builder.
Maintainers
Readme
The Problem
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/Your coworker asks what it does. You mass tears. You mass confusion. You mass quit.
The Solution
import { email } from 'zeroreg/patterns'
email.test('[email protected]') // trueOr build your own:
import { digit, optional } from 'zeroreg'
const phone = optional('+')
.then(digit(3))
.then('-')
.then(digit(3))
.then('-')
.then(digit(4))
phone.test('123-456-7890') // true
phone.toRegex() // /\+?\d{3}-\d{3}-\d{4}/Installation
npm install zeroregAPI
Character Classes
| Function | Description | Regex |
|----------|-------------|-------|
| digit(n?) | Match digits | \d or \d{n} |
| word() | Word characters | \w |
| letter() | Letters only | [a-zA-Z] |
| whitespace() | Whitespace | \s |
| any() | Any character | . |
| literal(str) | Exact match | (escaped) |
| charIn(chars) | Match any in set | [...] |
| charNotIn(chars) | Match any NOT in set | [^...] |
Quantifiers
| Method | Description |
|--------|-------------|
| .oneOrMore() | 1+ times |
| .zeroOrMore() | 0+ times |
| .optional() | 0 or 1 |
| .times(n) | Exactly n |
| .between(min, max) | Range |
| .atLeast(n) | n or more |
Groups
| Function | Description |
|----------|-------------|
| capture(pattern, name?) | Capturing group |
| group(pattern) | Non-capturing group |
| oneOf(...patterns) | Match any of |
Anchors
| Function | Description |
|----------|-------------|
| startOfLine() | ^ |
| endOfLine() | $ |
| wordBoundary() | \b |
Output
| Method | Description |
|--------|-------------|
| .toRegex(flags?) | Get native RegExp |
| .test(str) | Test string |
| .match(str) | Get matches |
| .matchAll(str) | Get all matches |
| .replace(str, replacement) | Replace matches |
Pre-built Patterns
import { email, url, phone, date, ipv4, uuid } from 'zeroreg/patterns'Available: email, url, phone, date, time, ipv4, ipv6, hexColor, hex, uuid, slug, hashtag, mention, creditCard, ssn, zipCode, username, strongPassword, semver, macAddress
License
MIT
