You can use the following regex, assuming the tool free number format is 00971800, followed by 5 or 8 digits. Your original regex is simplified with character classes. The test shows 4 valid numbers, followed by invalid numbers:
const regex = /^00971((5\d|[234679])\d{7}|800(\d{5}|\d{8}))$/;
[
'00971581234567',
'0097171234567',
'0097180012345',
'0097180012345678',
'0097158123456',
'009715812345678',
'009717123456',
'00971712345678',
'00971800123456',
'009718001234567',
].forEach((str) => {
let valid = regex.test(str);
console.log(str + ' ==> ' + valid);
});
Output:
00971581234567 ==> true
0097171234567 ==> true
0097180012345 ==> true
0097180012345678 ==> true
0097158123456 ==> false
009715812345678 ==> false
009717123456 ==> false
00971712345678 ==> false
00971800123456 ==> false
009718001234567 ==> false
Explanation:
^
- start of string
00971
- expect literal text
(
- start group, used for logical OR
(
- start group, used for logical OR
5\d
- expect a 5
, followed by a digit
|
- OR
[234679]
- character class with a single char of allowed digits
)
- end of group
\d{7}
- 7 digits
|
- OR
800
- literal text
(
- start group, used for logical OR
\d{5}
- 5 digits
|
- OR
\d{8}
- 8 digits
)
- end of group
)
- end of group
$
- end of string