3

I have a phone number formatted like this.+251-911-123456.

How could I create a regex that matches the given format.

I was trying to do it with the bellow given expression but it is not a success.

^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
Melaku Minas Kasaye
  • 640
  • 2
  • 8
  • 31

1 Answers1

1

You may use

/^\+\d{3}-\d{3}-\d{6}$/

Details

  • ^ - start of string
  • \+ - a + symbol
  • \d{3} - three digits
  • - - a hyphen
  • \d{3} - three digits
  • - - a hyphen
  • \d{6} - six digits
  • $ - end of string.

See the regex demo and the Regulex graph:

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563