I am trying to create a very simple engine that will generate some random data based on a token.
I was thinking that the input for the engine can be regex expressions so for example
engine will get [0-9] will yield any random number like 9 9999 0897 000
engine will get \w will yield any random word (even without meaning) like asdd gwasss ttt were khhu encyclopedia
so if I want a random email, I will write an expression:
\w@\w.com
for example random phone number
\d{3}-\d{3}-\d{4}
I am not sure how to approach this, Is there any JS library I can use to parse regex and override to get the output the way I want it? Or do I need to write my own parser?
pseudo code:
function getRandom('\d{3}-\d{3}-\d{4}');
will return
384-495-3344 or 433-244-3454 etc
Consider using an existing Regular Expression parser which outputs an AST .
For example for JavaScript:
https://www.npmjs.com/package/regjsparser
https://github.com/jviereck/regjsparser
The demo page here allows you to see the generated AST:
http://www.julianviereck.de/regjsparser/
Then you could look through the "type" in the AST, in this case this includes the "dot" type:
{
"type": "dot",
"range": [
4,
5
],
"raw": "."
},
Also note there is a JS library to generate regular expressions from the AST:
https://www.npmjs.com/package/regjsgen
https://github.com/bnjmnt4n/regjsgen
PS: I posted a similar answer to this for a different problem here https://stackoverflow.com/a/57096632/406712 it might be worth a look.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.