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?
2 Answers
Reset to default 6You 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条)