2017-02-28 269 views
-1

我在這裏有一個函數,用於檢查給定數組的正則表達式的元素。我傳遞的數組包含十個不同的正則表達式。計算正則表達式數組的長度

var regExAlphabet = /[a-z]/; 
var regExNumbers = /[0-9]/; 
var regExWhile = /while/; 
var regExIf = /if/; 
var regExElse = /else/; 
var regExTrue = /true/; 
var regExFalse = /false/; 
var regExInt = /int/; 
var regExString = /string/; 
var regExBoolean = /boolean/; 

var regexList = [regExAlphabet, regExNumbers, regExTrue, regExFalse, 
regExInt, regExString, regExBoolean, regExWhile, regExIf, regExElse]; 

function loopThroughOptions(regexList, element) { 
    for (var i = 0; i < regexList.length; i++) 
    failSafe(regexList[i], element) // failSafe is defined but not shown 
} 

var aString = "a"; 

loopThroughOptions(regexList, aString); 

當我運行它,我得到一個未捕獲的類型錯誤:無法讀取的未定義的屬性長度在我loopThroughOptions功能。這是爲什麼發生?我該如何解決它?

編輯:它看起來像我將需要發佈failSafe函數。這是相當長的。刺傷它。

var tokenList = []; // list of discovered tokens 
var substringsArray = []; // any strings that are not tokens go here 

    function substringsHandler(array) { 
    for (var i = 0; i < substringsArray.length; i++) { 
    for (var y = 0; y < regexList.length; y++) { 
     failSafe(regexList[y], substringsArray[i]) 
    } 
    } 
}  

function findAMatch(value) { 
    if (value == "a") 
     console.log("matched a"); 
} 

function findACharMatch(value) { 
    if (value == "a") 
     console.log("matched a"); 
} 

function failSafe(regEx, element) { 

    if (regEx.test(element) && element.length > 1) { // if the token is there 
    var x = regEx.exec(element); // give us more information on the element 
    var value = x["0"]; // keep track of the value of the token 
    var index = x.index; // keep track of the index 
    var substring = value; 
    console.log(index); 
    console.log(substring.length); 
    console.log(element.length); 
    tokenList.push({ 
     value: substring, 
     indexFound: index}); 
    console.log(tokenList[0]); 
    if (index > 0 && index + substring.length - 1 < element.length) { // if we found a token in the middle of a string 
     console.log("Found token in the middle of the string."); 
     substringsArray.push({ // give us the half that comes before the match 
      value: element.substring(0, index), 
      indexFound: 0 
      }); 

     substringsArray.push({ // give us the rest of the string that occurs after the match 
     value: element.substring(index + value.length), 
     indexFound: index + value.length 
     }); 

     substringsHandler(substringsArray); 
     // all successful token finds get sent to tokenList to search for a match 
     // if nothing is found, then everything gets translated to characters or digits 
    }  else if (index > 0 && index + substring.length - 1 == element.length) { // if there is more string to the left only 
      console.log("Found token on the right of the string."); 
      substringsArray.push({ 
      value: element.substring(0, index), // compare these values using find a match later 
      indexFound: 0 
      }) 
    } else if (index == 0 && substring.length < element.length) { // if there is  more string to the right only 
      console.log("Found token on the left of the string."); 
      substringsArray.push({ 
      value: element.substring(substring.length), 
      indexFound: substring.length 
      }) 
    } else { // the token is the only input 
     console.log("The token consists of the entire string."); 
    } 
    } else if (regEx.test && element.length == 1) { 
     var x = regEx.exec(element); // give us more information on the element 
     var value = x["0"]; // keep track of the value of the token 
     var index = x.index; // keep track of the index 
     var substring = value; 
     tokenList.push({ 
      value: value, 
      index: index 
     }) 
    } else { 
     console.log("No match for regexp " + regEx + "trying the next one..."); 
     return; 
    } 
    console.log(tokenList); 
    tokenList.sort(function(a, b) { 
    return a.indexFound - b.indexFound; 
    }); 
    console.log(tokenList); 
    for (var i = 0; i < tokenList.length; i++) { 
    if (tokenList[i].value.length > 1) 
     findAMatch(tokenList[i].value); 
    else 
     findACharMatch(tokenList[i].value); 
    } 
}; 
+0

運行代碼對我來說沒有問題。 – JohanP

+0

我也沒有看到問題,但它可能是範圍問題。此外,如果您將始終使用10個元素,您可以使用10個元素,或者創建一個全局變量,即var size = regexList.length;並迭代,直到我 Alan

+0

我也沒有得到一個問題,但我可能認爲'failSafe'函數做一些你的數組 –

回答

0

好了,我跑了所有的顯示的代碼,它有一個錯誤,根據RegExp docs

If the match fails, the exec() method returns null.

所以,在你的代碼,你總是想當然地認爲regEx.exec(element);會返回一個數組(它假設RegExp至少會匹配一個元素),至少在你的例子中,它是錯誤的,而你沒有處理它。

總之,要擺脫這種最簡單的方式是通過返回如果x爲null:

var x = regEx.exec(element); 
if (!x) return // add this 

進行了測試,並沒有什麼問題被拋出,只允許進行控制檯輸出是matched a