randexp.js
randexp will generate a random string that matches a given RegExp Javascript object.
Usage
const RandExp = ; // supports grouping and piping/hello+ /;// => hellooooooooooooooooooo world // sets and ranges and references/<>foo<\1>/;// => <m5xhdg>foo<m5xhdg> // wildcard/random stuff: .+/;// => random stuff: l3m;Hf9XYbI [YPaxV>U*4-_F!WXQh9>;rH3i l!8.zoh?[utt1OWFQrE ^~8zEQm]~tK // ignore case/xxx xtreme dragon warrior xxx/i;// => xxx xtReME dRAGON warRiOR xXX // dynamic regexp shortcut'(sun|mon|tue|wednes|thurs|fri|satur)day' 'i';// is the same as'(sun|mon|tue|wednes|thurs|fri|satur)day' 'i';
If you're only going to use gen()
once with a regexp and want slightly shorter syntax for it
const randexp = randexp; ; // 4; // great
If you miss the old syntax
; /yes|no|maybe|i don't know/; // maybe
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 language you can use to express the string you want?
Thanks to String-Random for giving me the idea to make this in the first place and randexp for the sweet .gen()
syntax.
Default Range
The default generated character range includes printable ASCII. In order to add or remove characters,
a defaultRange
attribute is exposed. you can subtract(from, to)
and add(from, to)
const randexp = /random stuff: .+/;randexpdefaultRange;randexpdefaultRange;randexp;// => random stuff: 湐箻ໜ䫴㳸長���邓蕲뤀쑡篷皇硬剈궦佔칗븛뀃匫鴔事좍ﯣ⭼ꝏ䭍詳蒂䥂뽭
You can also change the default range by changing RandExp.prototype.defaultRange
.
Custom PRNG
The default randomness is provided by Math.random()
. If you need to use a seedable or cryptographic PRNG, you
can override RandExp.prototype.randInt
or randexp.randInt
(where randexp
is an instance of RandExp
). randInt(from, to)
accepts an inclusive range and returns a randomly selected number within that range.
Infinite Repetitionals
Repetitional tokens such as *
, +
, 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 change the max
property in RandExp.prototype
or the RandExp instance.
const randexp = /no{1,}/;randexpmax = 1000000;
With RandExp.sugar()
const regexp = /*/;regexpmax = 1000000;
Bad Regular Expressions
There are some regular expressions which can never match any string.
-
Ones with badly placed positionals such as
/a^/
and/$c/m
. Randexp will ignore positional tokens. -
Back references to non-existing groups like
/(a)\1\2/
. Randexp will ignore those references, returning an empty string for them. If the group exists only after the reference is used such as in/\1 (hey)/
, it will too be ignored. -
Custom negated character sets with two sets inside that cancel each other out. Example:
/[^\w\W]/
. If you give this to randexp, it will return an empty string for this set since it can't match anything.
Projects based on randexp.js
JSON-Schema Faker
Use generators to populate JSON Schema samples. See: jsf on github and jsf demo page.
Install
Node.js
npm install randexp
Browser
Download the minified version from the latest release.
Tests
Tests are written with mocha
npm test
Integration with TypeScript
RandExp includes TypeScript definitions.
;;randexp.gen;
Use dtslint to check the definition file.
npm install -g dtslint
npm run dtslint