Launching a SaaS?·Collect waitlist signups before you build — before.run
SandboxPatternsDetectDocs
v0.1.0·MIT License·79 patterns

@regex.to/validators

A curated collection of battle-tested regex patterns with TypeScript types, Zod integration, and multi-language code generation. Zero runtime dependencies.

Installation

Install from npm. Zod is an optional peer dependency for @regex.to/validators/zod:

bash
npm install @regex.to/validators
pnpm add @regex.to/validators
yarn add @regex.to/validators

# with Zod integration
npm install @regex.to/validators zod

Basic Usage

typescript
import { test, validate, getRegex, PATTERNS } from '@regex.to/validators';

// Quick boolean test
test('email', 'user@example.com');  // → true
test('email', 'not-an-email');      // → false

// Full validation result
const result = validate('email', 'user@example.com');
// → { valid: true, pattern: PatternData, matches: [...] }

// Get the compiled RegExp directly
const re = getRegex('email');       // → /[a-zA-Z0-9._%+\-]+@.../i

// Access the pattern catalog
console.log(PATTERNS['email'].description);

Zod Integration

typescript
import { zodSchema, zodObjectSchema } from '@regex.to/validators/zod';
import { z } from 'zod';

// Single field schema
const emailSchema = zodSchema('email');
emailSchema.parse('user@example.com'); // ✓ passes
emailSchema.parse('invalid');          // ✗ throws ZodError

// Object schema from field map
const contactSchema = zodObjectSchema({
  email: 'email',
  website: 'url',
  ip: 'ipv4',
});

type ContactForm = z.infer<typeof contactSchema>;

detect() — Auto-detect string type

Pass any unknown string and get back an ordered list of matching patterns — full matches (entire string) ranked before partial matches.

typescript
import { detect } from '@regex.to/validators';

detect('user@example.com');
// → [{ pattern: { slug: 'email', name: 'Email Address', ... },
//      matchType: 'full', matchedText: 'user@example.com', coverage: 1 }]

detect('+14155552671');
// → [{ slug: 'phone-e164', matchType: 'full', coverage: 1 }, ...]

detect('0x742d35Cc6634C0532925a3b844Bc454e4438f44e');
// → [{ slug: 'eth-address', matchType: 'full', coverage: 1 }]

Results are sorted by confidence: full matches first, then by coverage ratio. Minimum match length is 4 characters and 20% coverage to filter trivial hits.

API Reference

FunctionDescription
test(slug, value)Quick boolean match
validate(slug, value)Full validation result with matches
getRegex(slug)Returns the compiled RegExp
detect(input)Auto-detect type of an unknown string
getAllPatterns()All patterns as an array
getByCategory(category)Filter patterns by category
getByTags(tags[])Filter patterns by tags
getPattern(slug)Single pattern by slug, or null
getSlugs()All available pattern slugs
getCategories()All available category names
zodSchema(slug, message?)Zod string schema (zod import)
zodObjectSchema(fieldMap)Zod object schema (zod import)

Patterns (79)

Identity16 patterns
au-abn

Australian ABN

Matches an Australian Business Number (ABN) — 11 digits, optionally space-separated in groups.

br-cnpj

Brazilian CNPJ

Matches a Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica) — company identifier in formatted or unformatted form.

br-cpf

Brazilian CPF

Matches a Brazilian CPF (Cadastro de Pessoas Físicas) number in formatted (NNN.NNN.NNN-NN) or unformatted (NNNNNNNNNNN) form.

cn-resident-id

Chinese Resident ID

Matches a Chinese national resident identity card number — 17 digits followed by one digit or X (checksum digit).

fr-siren

French SIREN Number

Matches a French SIREN (Système d'Identification du Répertoire des ENtreprises) number — 9 digits.

fr-siret

French SIRET Number

Matches a French SIRET (Système d'Identification du Répertoire des Établissements) number — 14 digits.

de-steuerid

German Tax ID (Steuer-IdNr)

Matches a German personal tax identification number (Steuerliche Identifikationsnummer) — 11 digits, first digit 1–9.

in-pan

Indian PAN Card

Matches an Indian Permanent Account Number (PAN) — 10 characters: 5 uppercase letters, 4 digits, 1 uppercase letter.

it-codice-fiscale

Italian Codice Fiscale

Matches an Italian fiscal code (Codice Fiscale) — 16 alphanumeric characters encoding name, birth date, gender, and municipality.

pl-pesel

Polish PESEL

Polish national identification number (PESEL) — an 11-digit number encoding date of birth, gender, and a checksum, assigned to every citizen.

pl-regon

Polish REGON

Matches a Polish REGON (National Business Registry Number) — 9 digits for companies, 14 digits for local units.

ru-inn

Russian INN (ИНН)

Matches a Russian taxpayer identification number (ИНН) — 10 digits for organisations, 12 digits for individuals.

es-nif

Spanish NIF / DNI

Matches a Spanish National Identity Document number (NIF/DNI) — 8 digits followed by a letter, or an NIE starting with X, Y, or Z.

uk-nino

UK National Insurance Number (NINO)

Matches a UK National Insurance Number in the format AA 99 99 99 A.

us-ein

US Employer Identification Number (EIN)

Matches a US EIN (Federal Tax ID) in the NN-NNNNNNN format used by the IRS.

us-ssn

US Social Security Number

Matches a US Social Security Number (SSN) in the standard NNN-NN-NNNN format.

Contributing a Pattern

All patterns are stored as small JSON files. Contributing is a 4-step process:

  1. 1

    Fork the repository

    Fork github.com/regex-to/validators and create a branch: pattern/your-pattern-name

  2. 2

    Create the JSON file

    json
    {
      "slug": "your-pattern",
      "name": "Human Readable Name",
      "description": "What this regex validates.",
      "category": "Internet",
      "pattern": "your[regex]pattern+",
      "flags": "i",
      "examples": ["valid-input-1", "valid-input-2"],
      "counterExamples": ["invalid-1"],
      "tags": ["tag1", "tag2"]
    }
  3. 3

    Test your pattern

    bash
    npm test
    # Or paste your pattern into the sandbox at regex.to
  4. 4

    Open a Pull Request

    Open a PR titled feat: add [pattern-name] pattern. Patterns are auto-discovered on merge.

Quick CLI Test

bash
node -e "
const { test, detect } = require('@regex.to/validators');
console.log(test('email', 'hello@world.com')); // true
console.log(detect('+48123456789')[0].pattern.slug); // phone-e164
"