gpt4 book ai didi

javascript - 为什么以下程序会为某些输入返回不正确的 bool 值?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:33:39 26 4
gpt4 key购买 nike

所以程序会检查输入的号码是否是有效的美国电话号码。

它广泛使用正则表达式。

通常,它会按预期工作。

但对于某些输入,它会返回无效的 bool 值。这是为什么?

输入:

  1. “1 (555) 555-5555”应该返回true,但实际上返回false。
  2. “(555)555-5555”应该返回true,但实际上返回false。
  3. “1(555)555-5555”应该返回true,但实际上返回false。
  4. “(6505552368)”应该返回false,但实际上返回true。
  5. “27576227382”应该返回false,但实际上返回true。
  6. "(555-555-5555"应该返回 false,但实际上返回 true。

以下是有效的美国电话号码格式列表:

  1. 555-555-5555
  2. (555)555-5555
  3. (555) 555-5555
  4. 555 555 5555
  5. 5555555555
  6. 1 555 555 5555

function telephoneCheck(str) {
// Good luck!
var regexArr = ["[0-9]{3}-[0-9]{3}-[0-9]{4}",
"([0-9]{3})[0-9]{3}-[0-9]{4}",
"1{1}([0-9]{3})[0-9]{3}-[0-9]{4}",
"[0-9]{3} [0-9]{3} [0-9]{4}",
"[1]{1} [0-9]{3} [0-9]{3} [0-9]{4}",
"1 ([0-9]{3}) [0-9]{3}-[0-9]{4}",
"[0-9]{10}"];

var cond = false;

for(var i = 0;i < regexArr.length;i++){
var regexObj = new RegExp(regexArr[i], "");
if(regexObj.test(str)){cond = true;
break;}
}

return cond;
}



telephoneCheck("555-555-5555");

最佳答案

这是一个单一的正则表达式解决方案,您可以查看它的railroad diagram here .

function telephoneCheck(str) {
return /^(?:1(-| ?)\d{3}\1\d{3}\1\d{4}|\d{3}(-| ?)\d{3}\2\d{4}|1? ?\(\d{3}\) ?\d{3}[- ]\d{4})$/.test(str);
}

console.log("Should pass:");
console.log("1 (555) 555-5555", telephoneCheck("1 (555) 555-5555"));
console.log("(555)555-5555", telephoneCheck("(555)555-5555"));
console.log("1(555)555-5555", telephoneCheck("1(555)555-5555"));
console.log("1-555-555-5555", telephoneCheck("1(555)555-5555"));
console.log("555-555-5555", telephoneCheck("555-555-5555"));
console.log("(555)555-5555", telephoneCheck("(555)555-5555"));
console.log("(555) 555-5555", telephoneCheck("(555) 555-5555"));
console.log("555 555 5555", telephoneCheck("555 555 5555"));
console.log("1 555 555 5555", telephoneCheck("1 555 555 5555"));
console.log("5555555555", telephoneCheck("5555555555"));
console.log("Should fail:");
console.log("(6505552368)", telephoneCheck("(6505552368)"));
console.log("27576227382", telephoneCheck("27576227382"));
console.log("(555-555-5555", telephoneCheck("(555-555-5555"));
console.log("1-(555)-555-5555", telephoneCheck("1-(555)-555-5555"));
console.log("1(555)5555555", telephoneCheck("1(555)5555555"));
console.log("1 555 5555555", telephoneCheck("1 555 5555555"));
console.log("1-555-5555555", telephoneCheck("1-555-5555555"));
console.log("1 555 555-5555", telephoneCheck("1 555 555-5555"));
console.log("1-555 555 5555", telephoneCheck("1-555 555 5555"));
console.log("1555-555-5555", telephoneCheck("1555-555-5555"));
console.log("1 (555 555-5555", telephoneCheck("1 (555 555-5555"));
console.log("1 555) 555-5555", telephoneCheck("1 555) 555-5555"));
console.log("1-5555555555", telephoneCheck("1-5555555555"));
.as-console-wrapper {
max-height: 100% !important;
}

如果有人可以缩短正则表达式,请随时发表评论,我会更新。

关于javascript - 为什么以下程序会为某些输入返回不正确的 bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44686782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com