randexp will generate a random string that matches a given RegExp Javascript object.
require('randexp'); // must require if using node
// supports grouping and piping
/hello+ (world|to you)/.gen;
// => hellooooooooooooooooooo world
// classes and ranges and references
/<([a-z]\w{0,20})>foo<\1>/.gen;
// => <m5xhdg>foo<m5xhdg>
// wildcard class
/random stuff: .+/.gen;
// => random stuff: 湐箻ໜ䫴㳸長���邓蕲뤀쑡篷皇硬剈궦佔칗븛뀃匫鴔事좍ﯣ⭼ꝏ䭍詳蒂䥂뽭
// ignore case
/xxx xtreme dragon warrior xxx/i.gen;
// => xxx xtReME dRAGON warRiOR xXX
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.
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._anyRandChar = 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;
npm install randexp
Download the minified version.
Tests are written with vows
npm test
I should really write browser tests too, sometime.
MIT