"reverse" regex with javascript (node.js)

With regular expressions, like this /\w/, can I compare the type of line a, q. Is there any idiomatic way to generate all strings that match some regular expression in JS?

Do not think of endless cases. I just want to briefly describe a few sets of possible characters.

something meaningful instead

var s = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'

+4
source share
1 answer

You can try the randexp library :

Randexp will generate a random string that matches this JavaScript RegExp object

See demo:

document.body.innerHTML = new RandExp(/\w/).gen();
document.body.innerHTML += "<br/>" + new RandExp(/\w/).gen();
document.body.innerHTML += "<br/>" + new RandExp(/[for]{3}/).gen();
document.body.innerHTML += "<br/>" + new RandExp(/I like (cats|dogs|mice)/).gen();
<script src="https://github.com/fent/randexp.js/releases/download/v0.4.1/randexp.min.js"></script>
Run codeHide result
+7

All Articles