Generate the simplest string that matches a given regular expression.
simpexp.js is a fork of randexp.js that will generate a simplest (shortest) string that matches a given RegExp, in a deterministic way (so that the same regex always generates the same string).
var SimpExp = require'simpexp';// supports grouping and piping/hello+ /gen;// => hello world// shorter is better/hello+ /gen;// => hello to u// a real-life example/<\s*br>/igen;// => <br>// sets and ranges and references/<>foo<\1>/gen;// => <a>foo<a>// 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
var simpexp = require'simpexp'simpexp;simpexp/[1-6]/; // 1simpexp'great|good( job)?|excellent'; // good
If you miss the old syntax
require'simpexp'sugar;/yes|no|maybe|i don't know/gen; // no
Generating a simplest string that matches a given regex helps you capture the essence of the regex, and therefore understand what it does. Additionally, when a regex is created to match various variations of a pattern, the simplest string that matches the regex is often the most natural form of the pattern.
This tool can also help you check correctness of complex regular expressions. By trying to minimize the length of the match, it may help expose loopholes in your regex that allow unintended strings to be matched.
Special thanks to Roly Fentanes and his
randexp.js library, on which this little
library is based. You can also find more information there.
npm install simpexp