randexp.js能帮助你生成符合某个正则表达式的随机字符串。
实现此库的动机:
正则表达式被用于每种语言,每位程序员都熟悉它们。正则表达式可以被轻易地用于表达复杂的字符串。而randexp.js能根据正则表达式,生成随机字符串。这是你想得到某种符合正则表达式的字符串的更好方式。
var RandExp = require('randexp');// supports grouping and pipingnew RandExp(/hello+ (world|to you)/).gen();// => hellooooooooooooooooooo world// sets and ranges and referencesnew RandExp(/<([a-z]\w{0,20})>foo<\1>/).gen();// => <m5xhdg>foo<m5xhdg>// wildcardnew RandExp(/random stuff: .+/).gen();// => random stuff: l3m;Hf9XYbI [YPaxV>U*4-_F!WXQh9>;rH3i l!8.zoh?[utt1OWFQrE ^~8zEQm]~tK// ignore casenew RandExp(/xxx xtreme dragon warrior xxx/i).gen();// => xxx xtReME dRAGON warRiOR xXX// dynamic regexp shortcutnew RandExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i');// is the same asnew RandExp(new RegExp('(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 itvar randexp = require('randexp').randexp;randexp(/[1-6]/); // 4randexp('great|good( job)?|excellent'); // great/// If you miss the old syntaxrequire('randexp').sugar();/yes|no|maybe|i don't know/.gen(); // maybe
评论