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
- Character classes
- 🎯 Configurable Length: Control approximate output length
- 🚀 Random Flags: Generates regex with random flags (
g,i,m,s,u,y)
Installation
npm install nv-random-rgxUsage
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 normallyUse 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.
