npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nv-random-rgx

v1.0.0

Published

A random regular expression generator for testing regex engines and generating diverse regex samples.

Downloads

8

Readme

nv-random-rgx

A random regular expression generator for testing regex engines and generating diverse regex samples.

Overview

nv-random-rgx is a specialized tool designed to generate valid, random regular expressions with maximum diversity. It's primarily used for:

  • Regex Engine Testing: Generate comprehensive test cases for regex engine implementations
  • Fuzzing: Create diverse regex patterns to test edge cases and corner cases
  • Sample Generation: Produce a wide variety of regex patterns for analysis and benchmarking

Features

  • Always Valid: Generates only syntactically correct JavaScript regular expressions
  • 🎲 High Diversity: Comprehensive coverage of regex syntax including:
    • Character classes [abc], [^a-z], [A-Z0-9]
    • Quantifiers *, +, ?, {n}, {n,}, {n,m}
    • Special escapes \d, \w, \s, \D, \W, \S
    • Unicode escapes \uXXXX, \xXX
    • Chinese characters and international text
    • Anchors ^, $
    • Alternation |
    • Metacharacters ., and more
  • 🎯 Configurable Length: Control approximate output length
  • 🚀 Random Flags: Generates regex with random flags (g, i, m, s, u, y)

Installation

npm install nv-random-rgx

Usage

const randomRegex = require('nv-random-rgx');

// Generate a random regex with default max length (128)
const regex1 = randomRegex();
console.log(regex1);
// Example output: /[a-z\d]+文|中\w{2,5}?[^A-Z]/gi

// Generate with custom max length
const regex2 = randomRegex(64);
console.log(regex2);
// Example output: /^\d+[abc\s]{3}?$/m

// Generate multiple samples
for (let i = 0; i < 10; i++) {
    console.log(randomRegex(256));
}

API

randomRegex(estimated_max_length)

Generates a random regular expression object.

Parameters:

  • estimated_max_length (number, optional): Approximate maximum length of the regex pattern. Default: 128
    • Note: The actual length may slightly exceed this value to ensure valid syntax

Returns:

  • RegExp: A valid JavaScript RegExp object with random pattern and flags

Example:

const rgx = randomRegex(100);
console.log(rgx.source);  // Get the pattern string
console.log(rgx.flags);   // Get the flags (e.g., "gi")
console.log(rgx.test('test string'));  // Use it normally

Use Cases

1. Testing Regex Engines

const testRegexEngine = (engine) => {
    for (let i = 0; i < 1000; i++) {
        const regex = randomRegex(200);
        try {
            engine.compile(regex.source);
            console.log(`✓ Test ${i}: ${regex.source}`);
        } catch (error) {
            console.error(`✗ Failed on: ${regex.source}`, error);
        }
    }
};

2. Fuzzing

const fuzzTest = () => {
    const testStrings = ['test', '123', 'αβγ', '测试', '🎉'];
    
    for (let i = 0; i < 100; i++) {
        const regex = randomRegex();
        testStrings.forEach(str => {
            try {
                regex.test(str);
            } catch (e) {
                console.error(`Error with ${regex} on "${str}":`, e);
            }
        });
    }
};

3. Benchmarking

const benchmark = () => {
    const regexes = Array.from({ length: 100 }, () => randomRegex(150));
    
    const start = Date.now();
    regexes.forEach(regex => {
        regex.test('benchmark string');
    });
    const end = Date.now();
    
    console.log(`Tested ${regexes.length} regexes in ${end - start}ms`);
};

Generated Patterns Include

  • Character classes: [a-z], [^0-9], [A-Za-z\d\s]
  • Quantifiers: *, +, ?, {3}, {2,}, {1,5}
  • Lazy quantifiers: *?, +?, ??, {2,5}?
  • Special character classes: \d, \w, \s, \D, \W, \S
  • Unicode escapes: \u4E00, \uFFFF, \x41, \xFF
  • International text: Chinese characters and more
  • Anchors: ^, $
  • Alternation: pattern1|pattern2|pattern3
  • Metacharacters: . (any character)

Notes

  • All generated regular expressions are guaranteed to be syntactically valid
  • Invalid patterns are never returned; if generation fails, a simple fallback regex /a*/ is used
  • The library focuses on diversity over semantic meaningfulness
  • Perfect for stress-testing and edge case discovery

License

MIT

Contributing

Issues and pull requests are welcome! This tool is designed for regex engine testing, so contributions that increase pattern diversity are especially appreciated.