What is randexp?
The randexp npm package is used to generate random strings that match a given regular expression. This can be useful for testing, generating mock data, or any scenario where you need random but structured strings.
What are randexp's main functionalities?
Generate Random String from Regex
This feature allows you to generate a random string that matches a given regular expression. In this example, the generated string will match the pattern 'hello+ (world|to you)'.
const RandExp = require('randexp');
const randexp = new RandExp(/hello+ (world|to you)/);
console.log(randexp.gen());
Custom Randomness
You can customize the randomness by overriding the `randInt` method. In this example, the `randInt` method is overridden to always return the lower bound, making the output deterministic.
const RandExp = require('randexp');
const randexp = new RandExp(/hello+ (world|to you)/);
randexp.randInt = (a, b) => a; // Always return the lower bound
console.log(randexp.gen());
Using Flags
RandExp supports regex flags such as case insensitivity. In this example, the 'i' flag makes the regex case insensitive.
const RandExp = require('randexp');
const randexp = new RandExp(/hello+ (world|to you)/i); // Case insensitive
console.log(randexp.gen());
Other packages similar to randexp
faker
Faker is a popular library for generating fake data. While it doesn't generate strings based on regular expressions, it provides a wide range of methods for generating random data such as names, addresses, and phone numbers. It is more feature-rich in terms of the variety of data it can generate compared to randexp.
chance
Chance is another library for generating random data. It offers a variety of random data generators, including strings, numbers, and even entire objects. Like Faker, it does not focus on regex-based string generation but provides a broader range of random data generation capabilities.
xeger
Xeger is a library specifically designed to generate strings that match a given regular expression, similar to randexp. It is a JavaScript port of the Java library Xeger and offers similar functionality to randexp, focusing on regex-based string generation.
randexp.js
randexp will generate a random string that matches a given RegExp Javascript object.
Usage
require('randexp');
/hello+ (world|to you)/.gen;
/<([a-z]\w{0,20})>foo<\1>/.gen;
/random stuff: .+/.gen;
/xxx xtreme dragon warrior xxx/i.gen;
Motivation
Regular expressions are used in every language, every programmer is familiar with them. Regex can be used to easily express complex strings. What better way to generate a random string than with a tool you can easily use to express the string any way you want?
Thanks to String-Random for giving me the idea to make this in the first place and randexp for the nifty little .gen
syntax.
Limitations
I wish I could say randexp is guaranteed to generate a string that will always match the given regex. But ONE limitation prevents me. Positionals. You can make a regex object that is guaranteed to never match any string. Such as
/a^b/m
That will never match any string because it will never be next to the beginning of the expression or a new line character. For now, positionals (^$\\b\\B
) are ignored. In the above case, randexp will generate the string ab
.
Classes like the .
character will match anything except a new line. In this case, a character with a random char code between 0 and 65535 will be generated. If you want to overwrite this function you can do
var r = /./;
r._anyRndChar = function() {
return the char you want here;
};
Ranges like *
, +
, and {3,}
have an infinite max range. In this case, randexp looks at its min and adds 100 to it to get a useable max value. If you want to use another int other than 100 you can do
var r = /(hi)*/;
r._max = 1000000;
Install
Node.js
npm install randexp
Browser
Download the minified version.
Tests
Tests are written with vows
npm test
I should really write browser tests too, sometime.
License
MIT