Regex - Oman Mobile No

Viewed 345

this regex is working ok for oman mobile number,s

^(\+968|968|00968)?\d{8}$

Oman Country Code +968 and 8 digit mobile number,

Mobile Numbers starts with 2,7,9 I want to edit this regex to force numbers starting with 2,7 & 9

I've tried to add [22-6]|[71-2]|[78-9]|[901-9]|[91-9] which are the mobile codes of different regions of oman.

^(\+968|968|00968)[2|7|9]?\d{8}$ tried to do it simple but it doesn't accepts mobile with out country codes it should accepts 8 number digit starting with 2 or 7 or 9

3 Answers

You can optionally match 00 or + followed by the county code using non capturing groups. Then match 2, 7, 9 and 7 digits

^(?:(?:\+|00)?968)?[279]\d{7}$
  • ^ Start of string
  • (?: Non capture group
    • (?:\+|00)? Optionally match + or 00
    • 968 Match literally
  • )? Close non capture group and make it optional
  • [279]\d{7} Match either 2, 7, 9 and 7 digits
  • $ End of string

Regex demo

with small modification in your regex.

(?:|968)[279]\d{7}$
Related