2017-02-28 271 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