6

With regular expression like this /\w/ I can match strings like a, q. Is there any idiomatic way to generate all the strings which match some regex in JS?

Don't think about infinite cases. I just want to describe some sets of possible symbols briefly.

something meaningful instead of

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

9
  • 3
    This is a hard problem to solve. As there are infinite regular expressions that have infinite strings they match (for example /a+/). Commented Mar 4, 2016 at 15:37
  • That program would very easy not halt anymore, a \w+ is all you need to generate an "infinite" amount of strings. Commented Mar 4, 2016 at 15:38
  • 2
    There is a PHP library who tries this. Commented Mar 4, 2016 at 15:41
  • 2
    And Python xeger, too. Oh, just came across randexp for JS. randexp will generate a random string that matches a given RegExp Javascript object. Commented Mar 4, 2016 at 15:42
  • 1
    Another link (regldg): This is a C based solution available for download and as web service (with restricted size of output) to test the functionality online. I have no affiliation whatsoever with the project. Commented Mar 4, 2016 at 16:13

1 Answer 1

9

You can try the randexp library:

Randexp will generate a random string that matches a given RegExp Javascript object

See the 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>

Sign up to request clarification or add additional context in comments.

5 Comments

You'd need to generate any chunk of text and test against the regexp. It is not possible nowadays, I think, you'd have to wait for ages. Why do you need to get all possible variations? You just need that for testing, don't you?
Again, that is not possible.
Why? I don't need to describe infinite cases. I just need to say /[\w\{\}}]/ instead of writing string like abcdef ..... {}
So, what prevents you from writing /[\w{}]/ manually? Regexps are meant to extract strings meeting specific patterns, not the other way round. The "other way round" is testing regexps, and what I suggest in the answer meets this end, I believe.
I mean to say that using the regexp the way you want is something what regex is not supposed to do. Saying "it is not possible" I mean it is not that easy to re-construct the regex parser (although it is feasible) and try to produce all substrings using the Unicode sets. Just that means a lot of work, and without some considerable effort, that is rather a broad topic for this Q&A site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.