I am trying to make a regular expression that matches Singapore phone numbers. The key part is that the numbers may or may not have spaces in between.
My requirements are:
- The number should start with
+65. - The next number should be either
6,8, or9. - The next 7 characters should be any digit from
0to9.
For example: +6581234567
I searched through Stack Overflow and found this question which does the exact same thing above.
Now like I said above, there could be spaces in between. And I'm not sure how to do the spaces. So the possible combinations are these two.
+6581234567+65 8123 4567
This will allow any whitespace symbol after
+65and before last 4 digits.^and$start and end of the string,\+65matches literal "+65",(\s?)matches an optional whitespace character. Caprured into group #1, and matched content will later be back referenced as\1,[689]matches any one of digits: 6, 8, or 9,\d{3}and\d{4}matches three and four digits accordingly,\1matches the same content that was in group #1.Demo here.