Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 电话号码正则表达式-合并三个条件_Javascript_Regex - Fatal编程技术网

Javascript 电话号码正则表达式-合并三个条件

Javascript 电话号码正则表达式-合并三个条件,javascript,regex,Javascript,Regex,我需要创建一个regex函数,该函数将根据特定条件验证电话号码字段,并显示三种情况下的警报 实际上,我有3个正则表达式函数,我想把它们合并成一个 /^3\d{9}$/; //If it starts with 3 and has another 9 numbers it's a cellphone /^0\d{7,10}$/; //If it starts with 0 and has another 7-10 numbers it's a landline /^(?:00|\+)/; //

我需要创建一个regex函数,该函数将根据特定条件验证电话号码字段,并显示三种情况下的警报

实际上,我有3个正则表达式函数,我想把它们合并成一个

/^3\d{9}$/; //If it starts with 3 and has another 9 numbers it's a cellphone

/^0\d{7,10}$/; //If it starts with 0 and has another 7-10 numbers it's a landline

/^(?:00|\+)/; //If it starts with 00 or a + sign, it's an international number
我试图实现的是一个javascript函数,它将结合这三个正则表达式函数,并在数字无效的情况下显示特定消息

例如,如果数字以3开头,但在3后面的数字少于或多于9,则可能是错误的手机号码,因此我想提醒用户这一点。固定电话也是这样。对于国际号码,我只是想让他们知道它可能是一个国际号码,因为它以双00或+号开头


我的问题是,我不知道如何将这三个正则表达式值组合成一个单一的值,这将允许我构建一个简单而干净的javascript代码。

我认为这对您有用:
/^(?:3\d{9}0\d{7,10}{124;(?:00}\+)\d+$/g

const testPhoneNumber=number=>/^(?:3\d{9}0\d{7,10}}测试(数字)
常数编号=[123,+48667065144,+311122333,、001234567、00123456879、6473812354、3475456389、7483925821]
for(常数个数){
log(`${number}是${testPhoneNumber(number)-“有效”:“无效”}电话号码`)
}
这将捕获阵列中的匹配数据(大小为4)

位置0:匹配的数据

位置1:第一个regexp的匹配数据

..

功能检查(编号){
//变量编号='3123456789';
//变量编号='01234567';
//变量编号='001';
var regexList=[
“^3\\d{9}$”,//如果它以3开头,又有9个号码,那就是手机
“^0\\d{7,10}$”,//如果它以0开头,并且有另外7-10个数字,则它是固定电话
“^[0]{2}{\+]”//如果它以00或+符号开头,则是一个国际数字
];
for(regexList中的var r)
if(number.match(新的RegExp(regexList[r]))
打破
其他的
r=null;
开关(r){
案例“0”:
警报(“如果它以3开头,又有9个号码,那就是手机”);
打破
案例“1”:
警报(“如果它以0开头,又有7-10个号码,则为固定电话”);
打破
案例“2”:
警报(“如果以00或+号开头,则为国际号码”);
打破
违约:
警报(“无效号码”);
}
}

尽我所能做到“优雅”

var input = '00372553253';
var matches = [];

// The following is technically one line!
[
    {regex: /^3\d{9}$/, type: "cellphone"},
    {regex: /^0\d{7,10}$/, type: "landline"},
    {regex: /^(?:00|\+)/, type: "international"}
].forEach(
    function(element) {
        if (element.regex.test(input))
            matches.push(element.type);
    }
);

alert(matches);

测试用例实际上将匹配两个正则表达式

好问题!我很想知道答案是什么。你可以通过将
|
放在三者之间来组合这三者,但我认为你所描述的“模糊逻辑”需要比这更复杂。如果这三者都失败(当你组合它们时),你就可以看到哪一个失败了。我喜欢你在这里的精神,但最好是单独检查它们,这样你才能给出充分的回答。听起来你根本不需要一个正则表达式,而是需要多个可以做不同事情的条件。Brian Kernihan写道“调试的难度是最初编写代码的两倍。因此,如果你尽可能巧妙地编写代码,根据定义,你就没有足够的智慧来调试它。”-换句话说,使用更容易访问的版本,而不是更优雅的版本!
var input = '00372553253';
var matches = [];

// The following is technically one line!
[
    {regex: /^3\d{9}$/, type: "cellphone"},
    {regex: /^0\d{7,10}$/, type: "landline"},
    {regex: /^(?:00|\+)/, type: "international"}
].forEach(
    function(element) {
        if (element.regex.test(input))
            matches.push(element.type);
    }
);

alert(matches);