🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

human-regex

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

human-regex

Human-friendly regex builder with English-like syntax

Source
npmnpm
Version
2.0.3
Version published
Maintainers
1
Created
Source

npm version Build Status Coverage Status

Human Regex 🤖➡️👤

Human-friendly regular expression builder with English-like syntax.

Installation

npm install human-regex

Usage

import { createRegex } from "human-regex";

// Simple password validation
const passwordRegex = createRegex()
  .digit() // Must contain digit
  .special() // Must contain special char
  .range("alphanumeric") // Must contain alphanumeric char
  .atLeast(8) // Must be at least 8 characters long
  .toRegExp(); // Convert to RegExp object

API Reference

createRegex()

Creates a new regex builder instance.

Methods

  • .literal(text: string): Adds a literal string to the pattern.
  • .digit(): Adds a digit pattern (\d).
  • .word(): Adds a word character pattern (\w).
  • .range(name: string): Adds a predefined character range.
  • .atLeast(n: number): Specifies that the preceding element must appear at least n times.
  • .exactly(n: number): Specifies that the preceding element must appear exactly n times.
  • .oneOrMore(): Specifies that the preceding element must appear one or more times.
  • .optional(): Specifies that the preceding element is optional.
  • .zeroOrMore(): Specifies that the preceding element can appear zero or more times.
  • .startGroup(): Starts a non-capturing group.
  • .endGroup(): Ends a non-capturing group.
  • .startAnchor(): Adds a start-of-string anchor (^).
  • .endAnchor(): Adds an end-of-string anchor ($).
  • .global(): Adds the global flag (g).
  • .nonSensitive(): Adds the case-insensitive flag (i).
  • .multiline(): Adds the multiline flag (m).
  • .dotAll(): Adds the dot-all flag (s).
  • .unicode(): Adds the Unicode flag (u).
  • .sticky(): Adds the sticky flag (y).
  • .toRegExp(): Converts the builder to a RegExp object.

Examples

Combining with Existing Regex

const basePattern = /^[A-Z]/;
const combined = createRegex().regex(basePattern).digit().exactly(3).toRegExp();

Lookaheads/Lookbehinds

createRegex().literal("(?<=@)").word().oneOrMore().toRegExp();

Named Capture Groups

createRegex()
  .literal("(?<year>\\d{4})")
  .literal("-")
  .literal("(?<month>\\d{2})")
  .toRegExp();

Contributing

We welcome contributions! Please follow the guidelines in CONTRIBUTING.md.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Keywords

regex

FAQs

Package last updated on 01 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts