Chrispinus Onyancha Chrispinus Onyancha - 1 month ago 13
Javascript Question

What regex expression would i use for 07X XXXX XXX as a number?

I have a phone number that i want the figures 07 to be constants then the rest should be regex?

Mine looks like this:

/^\07(?([0-9]{1})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/
.

Example Number: 0752 8182 12

Answer

I suggest fixing your pattern as

/^07\(?([0-9])\)?[-. ]?([0-9]{4})[-. ]?([0-9]{3})$/

See the regex demo

You missed the \ before the first ( and the backslash with \07 made the regex engine parse the 07 part as an octal value.

Pattern details:

  • ^ - start of string
  • 07 - 07
  • \(? - an optional ( (note that you need to remove it together with \)? if you do not want to allow values like 07(5) 4444 333)
  • ([0-9]) - a digit (the ( and ) may be removed if you do not need to get the captured subtext)
  • \)? - an optional )
  • [-. ]? - an optional -, . or space
  • ([0-9]{4}) - 4 digits (same note about parentheses)
  • [-. ]? - ibid
  • ([0-9]{3}) - 3 digits (same note about parentheses)
  • $ - end of string.