simpexp.js
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).
Usage
var SimpExp = ; // supports grouping and piping/hello+ /;// => hello world // shorter is better/hello+ /;// => hello to u // a real-life example/<\s*br>/i;// => <br> // sets and ranges and references/<>foo<\1>/;// => <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 = simpexp; ; // 1; // good
If you miss the old syntax
; /yes|no|maybe|i don't know/; // no
Motivation
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.
randexp.js
Special thanks to Roly Fentanes and his randexp.js
library, on which this little
library is based. You can also find more information there.
Install
Node.js
npm install simpexp
License
MIT