问题
so I'm trying to make this Regex allow this the Dash symbol - For Example this Phone Number is not matching right now
+212 659-123456
So I need someone to help me change the Regex to allow it please Here is the Regex:
^\+(?:[0-9]\x20?){6,14}[0-9]$
Because I am trying to only accept the format that is used by WhatsApp and some numbers might have multiple spaces or multiple Dashes. Also the Plus sign has to be mandatory Here some more examples of the format on WA.
+96274567123
+967773-123-123
+212 627-024321
+212689-881234
+966 54 666 4373
The numbers above cover 99% of the cases. I would appreciate any help, thanks and regards
回答1:
I would just use:
^(?=(?:[+ -]*[0-9][+ -]*){11,12}$)\+(?:[0-9]+[ -]?)+[0-9]$
Explanation:
(?=(?:[+ -]*[0-9][+ -]*){11,12}$)
Positive lookahead which checks that the string has exactly 11 or 12 digits in it.\+(?:[0-9]+[ -]?)+[0-9]
Has to start with a+
and end with a digit, in between can be groups of one ore more digits plus optionally a single-
.
regex101 demo
回答2:
^\+([\s\-0-9]){6,14}$
This would catch all your entries. It would be easier if you delete all whitespaces and unwanted characters and test than. Especially when the String to test becomes longer and longer because of whitespaces.
来源:https://stackoverflow.com/questions/65094543/how-to-allow-only-whatsapp-format-numbers-in-a-regex