Try this
/^\s*(?:\+44(?:\s*\(\s*0\s*\))?|0)\s*(7(?:\s*\d){9}|(?=\d)[^7](?:\s*\d){8,9})\s*$/
// ^-------------A------------^ ^-----B-----^ ^-----------C----------^
- A is international/domestic code, i.e.
+44
, 0
or +44 (0)
, consider adding 0044
, 0044 (0)
- B is mobile phone number,
7
followed by 9 more digits
- C is a landline number, non-7 followed by 8 or 9 more digits, consider breaking into region groups
Spaces are permitted throughout the whole thing except in the +44
There is a capture group which will give you the result of either B or C, depending on the match
Examples
var re = /^\s*(?:\+44(?:\s*\(\s*0\s*\))?|0)\s*(7(?:\s*\d){9}|(?=\d)[^7](?:\s*\d){8,9})\s*$/;
// Mobile
'+44 (0) 712 345 6789'.match(re); // ["+44 (0) 712 345 6789", "712 345 6789"]
'+44 712 345 6789'.match(re); // ["+44 712 345 6789", "712 345 6789"]
'0712 345 6789'.match(re); // ["0712 345 6789", "712 345 6789"]
// Landline
'020 4 953 3525'.match(re); // ["020 4 953 3525", "20 4 953 3525"]
'016 977 3355'.match(re); // ["016 977 3355", "16 977 3355"]
Useful RegExp patterns used here
(?:pattern)
Non-capture group; This pattern is a group but not captured seperately
(?:\s*\d){9}
Any amount of whitespace followed by a number, 9 times
(?=pattern)
Lookahead; Look to see if pattern
would match next, but don't match it
(?=\d)[^7]
, If the next character is a digit match the next character if it isn't a 7