簡體   English   中英

為什么以下程序為某些輸入返回錯誤的布爾值?

[英]Why does the following program return incorrect boolean values for some inputs?

因此,程序將檢查輸入的號碼是否為有效的美國電話號碼。

它廣泛使用正則表達式。

通常,它可以正常工作。

但是對於某些輸入,它將返回無效的布爾值。 這是為什么 ?

輸入:

  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"); 

這是一個正則表達式解決方案,您可以在此處查看其鐵路圖

 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; } 

如果有人可以縮短正則表達式,請隨時發表評論,我會進行更新。

您需要一個反斜杠\\來避免圓括號(否則它們將被解釋為分組符號)和另一個反斜杠,因為您將正則表達式括在引號中,因此\\\\(

另外,您可以嘗試將不同的正則表達式統一為一個,例如use

"1\\s*\\([0-9]{3}\\)\\s*[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}"

 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}", "1\\\\s*\\\\([0-9]{3}\\\\)\\\\s*[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; } console.log(telephoneCheck("1 (555) 555-5555")); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM