[英]regex for all UK telephone numbers
I am trying to validate UK telephone numbers, which are in the format of:我正在尝试验证英国电话号码,其格式为:
01234 567890
01234567890
02012345678
020 1234 5678
I have the following regex, which works for all apart from the 020 1234 5678
我有以下正则表达式,它适用于除020 1234 5678
之外的所有人
^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$
Does anyone know why this is?有人知道为什么吗?
You are missing a closing square bracket您缺少右方括号
^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$
^
here
This should be working这应该工作
^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$
Further you can remove some stuff from your expression.此外,您可以从表达式中删除一些内容。
{1}
is not need every character is still matched once {1}
不需要每个字符仍然匹配一次
[ ]
is also not needed, just replace it with a space [ ]
也不需要,只需用空格替换即可
[8,9]
is wrong. [8,9]
是错误的。 it will match 8, 9 and, .它将匹配 8、9 和 . Use [89]
is correct.使用[89]
是正确的。
Then it would look like that然后它看起来像这样
^\s*\(?(020[78]\)? ?[1-9][0-9]{2} ?[0-9]{4})|(0[1-8][0-9]{3}\)? ?[1-9][0-9]{2} ?[0-9]{3})\s*$
If you want to allow this not matched pattern 020 1234 5678 you could do for example如果你想允许这个不匹配的模式020 1234 5678你可以做例如
^\s*\(?(020[78]?\)? ?[1-9][0-9]{2,3} ?[0-9]{4})$|^(0[1-8][0-9]{3}\)? ?[1-9][0-9]{2} ?[0-9]{3})\s*$
^ ^^
made the [78] optional allows no 2 or 3 of `[0-9]`
See it here on Regexr在 Regexr 上查看
I have no clue if this is a valid UK phone number!我不知道这是否是一个有效的英国电话号码!
I fixed here also another bug in the regex a missing $
before and a missing ^
after the pipe |
我在这里还修复了正则表达式中的另一个错误,即 pipe |
之前缺少$
和之后缺少^
。
If you can use Perl Number::Phone::UK can check if a number is valid, allocated, mobile, personal or business... and so on.如果您可以使用 Perl Number::Phone::UK可以检查号码是否有效、已分配、移动、个人或企业...等等。
My JS solution我的 JS 解决方案
var total = 0;
var passed = 0;
var failed = 0;
var r = /^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?$/;
var pass = [
'(0123) 456 7890',
'012 3456 7890',
'+44 12345 67890',
'+44 1234567890',
'0044 1234 567 890',
'0044 1234 567890',
];
pass.each ( function (value) {
var result = r.test( value );
console.log( value, (result == true ? 'ok' : 'failed') );
total ++;
if ( result == true ) {
passed ++;
} else {
failed ++;
}
} );
var fail = [
'(0123) 456 78',
'012 3456 789',
'+44 12345 678',
'0044 1234 567 89',
];
fail.each ( function (value) {
var result = r.test( value );
console.log( value, (result == false ? 'ok' : 'failed') );
total ++;
if ( result == false ) {
passed ++;
} else {
failed ++;
}
} );
setTimeout(function () {
console.log( "PASSED " + passed + ' / ' + total );
}, 500);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.