根据正则表达式生成随机字符串 [英] Generate a random string based on a regular expression

查看:76
本文介绍了根据正则表达式生成随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以生成满足正则表达式的随机文本.
我正在寻找一个功能,如下所示

Is there any way of generating random text which satisfies provided regular expression.
I am looking for a function which works like below

var reg = Some Regular Expression  
var str = RandString(reg)

推荐答案

我在 perl中看到了相当不错的解决方案 ruby​​ 在github上,但我认为有些技术问题使完整的解决方案无法实现.例如,/[0-9] +/的上限是无限的,这对于从中选择随机数不可行.

I have seen fairly good solutions in perl and ruby on github, but I think there are technical issues that make a complete solution impossible. For example, /[0-9]+/ has an infinite upper bound, which is not practical for selecting random numbers from.

从未在JavaScript中看到过,但是您可以翻译.

Never seen it in JavaScript, but you could translate.

谷歌搜索几秒钟后... http://fent.github.com/randexp.js/

After googling for a few seconds... http://fent.github.com/randexp.js/

这篇关于根据正则表达式生成随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆