Skip to content

Human-readable regex builder for JavaScript & TypeScript

Notifications You must be signed in to change notification settings

ZenWeb3/zeroReg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zeroReg

Regex was a mistake. Just like your ex.

Write regex without the tears or confusion.
A human-readable regex builder for JavaScript & TypeScript.

npm version npm downloads license bundle size


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('hello@world.com') // true

Or 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 zeroreg

API

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

About

Human-readable regex builder for JavaScript & TypeScript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors