问题
How can i validate a phone number with plus sign optional only at the beginning and after that any number of digits with number only.
I tried this:-
/^\+(?:[\d]*)$/
How will be the modification
回答1:
/^\+?(?:[\d]*)$/
The questionmark tells that the plus sign can be there or not. However, your expression can be optimized quite a bit:
/^\+?\d+$/
I changed the *
to a +
as the expression would match just a plus sign. \d*
suggests that it should match 0 or more digits.
Here's a demo on Regexr.
来源:https://stackoverflow.com/questions/29555521/phone-number-validation-with-plus-sign-optional