jquery - Generate string for using Regular expression on Javascript - Stack Overflow

I'm making a faker plugin ( jQuery-Faker ) in jQuery which generates fake data for fill in the for

I'm making a faker plugin ( jQuery-Faker ) in jQuery which generates fake data for fill in the form corresponding to their field name. 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?

I'm making a faker plugin ( jQuery-Faker ) in jQuery which generates fake data for fill in the form corresponding to their field name. 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?

Share Improve this question edited Jul 3, 2015 at 21:25 ndnenkov 36.1k9 gold badges79 silver badges107 bronze badges asked Jul 3, 2015 at 20:45 Bibek SharmaBibek Sharma 3,3307 gold badges43 silver badges65 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You might want to look at the string from regex family of libraries.

Here is the one for javascript

As seen in the README you can do

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()

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744993577a4605051.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信