简体   繁体   English

生成字符串以在Javascript上使用正则表达式

[英]Generate string for using Regular expression on Javascript

I'm making a faker plugin ( jQuery-Faker ) in jQuery which generates fake data for fill in the form corresponding to their field name. 我正在jQuery中制作一个faker插件( jQuery-Faker ),它生成伪造数据以填充与其字段名称对应的表单。 So I want to generate phone number using regular expression /\\(?([0-9]{3})\\)?([ .-]?)([0-9]{3})\\2([0-9]{4})/ So is there any way to do this? 所以我想用正则表达式生成电话号码/\\(?([0-9]{3})\\)?([ .-]?)([0-9]{3})\\2([0-9]{4})/那么有什么办法可以做到这一点吗?

You might want to look at the string from regex family of libraries. 您可能希望查看来自regex系列库的字符串。

Here is the one for javascript 这是javascript的一个

As seen in the README you can do 如您在README中所见,您可以这样做

var RandExp = require('randexp');
var phoneGenerator = new RandExp(/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/);
phoneGenerator.gen(); // =>

Try utilizing String.prototype.replace() 尝试使用String.prototype.replace()

 var n = "01234567890".split("") , ph = "nnn-nnn-nnnn".replace(/n/g, function() { var i = Math.floor(Math.random() * n.length); return n.slice(i, i + 1)[0] }); document.write(ph); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM