randexp.js - JavaScript 随机字符串库


MIT
跨平台
JavaScript

软件简介

randexp.js 能帮助你生成符合某个正则表达式的随机字符串。

实现此库的动机:

正则表达式被用于每种语言,每位程序员都熟悉它们。正则表达式可以被轻易地用于表达复杂的字符串。 而 randexp.js
能根据正则表达式,生成随机字符串。这是你想得到某种符合正则表达式的字符串的更好方式。

var RandExp = require('randexp');

// supports grouping and piping
new RandExp(/hello+ (world|to you)/).gen();
// => hellooooooooooooooooooo world

// sets and ranges and references
new RandExp(/<([a-z]\w{0,20})>foo<\1>/).gen();
// => <m5xhdg>foo<m5xhdg>

// wildcard
new RandExp(/random stuff: .+/).gen();
// => random stuff: l3m;Hf9XYbI [YPaxV>U*4-_F!WXQh9>;rH3i l!8.zoh?[utt1OWFQrE ^~8zEQm]~tK

// ignore case
new RandExp(/xxx xtreme dragon warrior xxx/i).gen();
// => xxx xtReME dRAGON warRiOR xXX

// dynamic regexp shortcut
new RandExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i');
// is the same as
new 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 it

var randexp = require('randexp').randexp;

randexp(/[1-6]/); // 4
randexp('great|good( job)?|excellent'); // great


/// If you miss the old syntax
require('randexp').sugar();

/yes|no|maybe|i don't know/.gen(); // maybe