Generate string to use regular expression in Javascript
I am creating a faker ( jQuery-Faker ) plugin in jQuery that generates fake form data to match their field name, So I want to generate a phone number using regex /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/
So is there a way to do this?
+3
source to share
2 answers
Try to use 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);
+3
source to share