简体   繁体   中英

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. 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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