JavaScript 隨機字符串庫:randexp.js

jopen 9年前發布 | 22K 次閱讀 JavaScript開發 randexp.js

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

項目主頁:http://www.baiduhome.net/lib/view/home/1441112020206

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!