Documentation

Everything you need to write regex like a normal person. No PhD required.

Language:

# Quick Start

Installation

npm install zeroreg

# or
pnpm add zeroreg
yarn add zeroreg
bun add zeroreg

Your first pattern

import { digit } from 'zeroreg'

// Match a phone number: 123-456-7890
const phone = digit(3).then('-').then(digit(3)).then('-').then(digit(4))

phone.test('123-456-7890')  // true
phone.toRegex()             // /\d{3}-\d{3}-\d{4}/

Or use pre-built patterns

import { email, url, phone } from 'zeroreg/patterns'

email.test('hello@world.com')     // true
url.test('https://github.com')    // true
phone.test('+1-234-567-8900')     // true

# Character Classes

Match specific types of characters.

FunctionDescriptionRegex
digit(n?)Match digits\\d or \\d{n}
word()Word char (a-z, 0-9, _)\\w
letter()Any letter a-z, A-Z[a-zA-Z]
whitespace()Whitespace\\s
any()Any character.
literal(str)Exact match (escaped)str
charIn('abc')Any char in set[abc]
charNotIn('abc')Any char NOT in set[^abc]
range('a', 'z')Character range[a-z]

Examples

import { digit, letter, charIn, literal } from 'zeroreg'

// Match exactly 3 digits
digit(3).test('123')  // true

// Match a vowel
charIn('aeiou').test('e')  // true

// Match a literal dot (escaped automatically)
literal('.').test('.')  // true

# Quantifiers

Control how many times a pattern matches.

MethodDescriptionRegex
.oneOrMore()1 or more+
.zeroOrMore()0 or more*
.optional()0 or 1?
.times(n)Exactly n times{n}
.between(min, max)Between min and max{min,max}
.atLeast(n)n or more times{n,}

Examples

import { digit, letter, optional } from 'zeroreg'

// One or more digits
digit().oneOrMore().test('123')  // true

// Optional plus sign, then digits
optional('+').then(digit().oneOrMore()).test('+123')  // true
optional('+').then(digit().oneOrMore()).test('123')   // true

// Between 2 and 4 letters
letter().between(2, 4).test('abc')  // true

# Groups

Capture parts of your match or group patterns together.

FunctionDescriptionRegex
capture(pattern)Capturing group(...)
capture(pattern, 'name')Named capture(?<n>...)
group(pattern)Non-capturing group(?:...)
oneOf(a, b, c)Match any of(?:a|b|c)

Examples

import { capture, digit, oneOf, group, literal } from 'zeroreg'

// Extract year, month, day
const datePattern = capture(digit(4), 'year')
  .then('-')
  .then(capture(digit(2), 'month'))
  .then('-')
  .then(capture(digit(2), 'day'))

const match = '2024-03-15'.match(datePattern.toRegex())
match.groups.year   // '2024'
match.groups.month  // '03'
match.groups.day    // '15'

// Match "cat" or "dog" or "bird"
oneOf('cat', 'dog', 'bird').test('dog')  // true

# Anchors

Match positions in the string, not characters.

FunctionDescriptionRegex
startOfLine()Start of string^
endOfLine()End of string$
wordBoundary()Word boundary\\b

# Output Methods

Convert your pattern to different formats and use it.

MethodReturns
.toRegex(flags?)Native RegExp object
.test(str)boolean — does it match?
.match(str)First match or null/None
.matchAll(str)Array/list of all matches
.replace(str, replacement)String with replacements

Examples

import { digit } from 'zeroreg'

const pattern = digit().oneOrMore()

// Test if string matches
pattern.test('abc123')  // true

// Get all matches
pattern.matchAll('abc 123 def 456')  // [['123'], ['456']]

// Replace all matches
pattern.replace('abc 123 def 456', 'X')  // 'abc X def X'

// Get native RegExp with flags
pattern.toRegex('gi')  // /\d+/gi

# Pre-built Patterns

Import ready-to-use patterns for common use cases.

import { email, url, phone, date, uuid, ... } from 'zeroreg/patterns'
email

user@domain.com

url

https://example.com

phone

+1-234-567-8900

date

2024-03-15

time

14:30:00

ipv4

192.168.1.1

uuid

550e8400-e29b-...

hexColor

#ff6600

slug

my-post-title

hashtag

#trending

mention

@username

creditCard

4111111111111111

username

user_123

strongPassword

MyP@ssw0rd

semver

1.2.3-alpha

macAddress

00:1A:2B:3C:4D:5E