Generate strings to fulfill basic regular expressions using rGen.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

rGen: regex fulfillment

Generate strings to fulfill basic regular expressions using rGen.

In other words: rGen produces random strings that comply with any given regular expression.


rGen(/hello/) // => "hello"
rGen(/\d\w\d/) // => "0f4"
rGen(/[a-z]+/) // => "cykifsqhioquvdquw"

// Note: it won't progress through possible strings
// It'll just give you random generations, but,
// ...eventually, something like:
// would produce:
//  => "cat"
//  => "car"
//  => "cot"
//  => "cor"

Note: rGen assumes that you really mean ^(?:___)$, otherwise rGen(/x/) would have to produce any random string that contains x and I'm not sure that's very useful...

What's it good for?

  • Password generators
  • Testing data
  • Fun

Currently supporting:

  • Character classes plus ranges (e.g. [a-z_])
  • Negated character classes
  • Shorthand character classes (supported: \d\D\s\S\W\w)
  • Infinite (theoretically) nested groups
  • Quantifiers (all quantifiers are treated as lazy, even if you add the ?)
  • + and * and {n,} have a hard-coded upper limit of 50 (TODO: make it configurable)

Possible future additions (???)

  • \b and \B
  • Lookarounds
  • Regex flags like i and m
  • ???

Other TODOs

  • Use headless PhantomJS w/ grunt to test in node
  • Add it to npm?