Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression - also empty string allowed

I have the following regular expression... This one does not allow empty String. How do I have to manipulate?

var phoneReg = new RegExp(/^\+?[0-9]+(\([0-9]+\))?[0-9-]*[0-9]$/i);
like image 832
user2286914 Avatar asked Dec 13 '25 21:12

user2286914


1 Answers

Just use the "zero or one" operator ?, such as:

/^(?:regex)?$/
           ^

(?:regex) simply makes the concerned ground uncapturable (i.e. no need to change your $X, \X or ?X indexes if any).

like image 147
sp00m Avatar answered Dec 15 '25 09:12

sp00m



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!