Create a regex-based random string

Is there a way to generate random text that satisfies the provided regular expression. I am looking for a function that works as shown below

var reg = Some Regular Expression var str = RandString(reg) 
+7
source share
4 answers

I saw pretty good solutions in perl and ruby on github, but I think there are technical problems that make a complete solution impossible. For example, / [0-9] + / has an infinite upper bound, which is impractical for choosing random numbers from.

Never seen it in JavaScript, but you could translate.


EDIT: After searching the Internet for a few seconds ... http://fent.github.com/randexp.js/

+12
source

if you know what a regular expression is, you can just generate random strings, and then use a function that refers to the index of letters and modifies them as needed. Expressions of regular expressions vary greatly, so it is difficult to find them, in particular, that satisfy all possible regular expressions.

+1
source

Your question is quite open, so I hope this leads you to the right solution. Get the current time (in seconds), MD5, check it against REGEX, return a match.

Execution example: http://jsfiddle.net/MattLo/3gKrb/

Usage: RandString(/([A-Za-z])/ig); // expected to be a string RandString(/([A-Za-z])/ig); // expected to be a string

+1
source

For JavaScript, the following modules can generate a random match with a regular expression:

0
source

All Articles