Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.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数组拒绝返回indexOf,即使在匹配发生时也是如此_Javascript_Arrays_Indexof - Fatal编程技术网

Javascript数组拒绝返回indexOf,即使在匹配发生时也是如此

Javascript数组拒绝返回indexOf,即使在匹配发生时也是如此,javascript,arrays,indexof,Javascript,Arrays,Indexof,所以我遇到了一个可以想象到的最奇怪的问题,我有一个javascript数组,它包含许多姓氏,它应该遍历文本,通过循环给出的单词并将它们放入数组的索引中来选择名称,看看它是否是非负的,但是,我一开始在拆分数组时犯了一个错误,其中,我在regex\s上拆分,在每个常规字符串之间有一个空白字符串。 有趣的是,一个确实可以工作,但当我将其固定为拆分时\n,数组是相同的减去空字符串,但是现在它拒绝通过indexOf匹配给定的相同输入。我在控制台中设置了一个断点来亲自检查它,但我发现了最奇怪的情况,我还没有

所以我遇到了一个可以想象到的最奇怪的问题,我有一个javascript数组,它包含许多姓氏,它应该遍历文本,通过循环给出的单词并将它们放入数组的索引中来选择名称,看看它是否是非负的,但是,我一开始在拆分数组时犯了一个错误,其中,我在regex\s上拆分,在每个常规字符串之间有一个空白字符串。 有趣的是,一个确实可以工作,但当我将其固定为拆分时\n,数组是相同的减去空字符串,但是现在它拒绝通过indexOf匹配给定的相同输入。我在控制台中设置了一个断点来亲自检查它,但我发现了最奇怪的情况,我还没有遇到下面的图片

我有很多编码经验,但我不知道这里发生了什么,请帮助

这是代码供参考

function findPeople(text,nameList){
    text=text.replace(/[^a-z0-9\s]/gi, " ");
    text=text.replace(/\s+/gi," ");
    var spliText=text.split(/\s+/gi);
    nameList=nameList.split("|")
    var fnames=nameList[0].split(/\s/gi);
    var lnames=nameList[1].split(/\s/gi);
    console.log(fnames.slice(0,15),lnames.slice(0,15))
    var matches = [];
    for (var i=0;i<spliText.length;i++){
        var word1=spliText[i].toLowerCase();
        try{var word2=spliText[i+1].toLowerCase();}catch(e){word2="NOPE";}
        try{var word3=spliText[i+2].toLowerCase();}catch(e){word3="NOPE";}
        try{var word4=spliText[i+3].toLowerCase();}catch(e){word4="NOPE";}
        if (fnames.indexOf(word1) != -1 && lnames.indexOf(word3) != -1 && word1!=word3 && !/(st|ln|lane|rd|road|dr|blv|cir|way|wy|pass)(?![a-z])/i.test(word4) && matches.indexOf(word1 + " " + word3) == -1){
              matches.push(word1 + " " + word3);
        }else if (fnames.indexOf(word1) != -1 && lnames.indexOf(word2) != -1 && word1!=word2 && !/(st|ln|lane|rd|road|dr|blv|cir|way|wy|pass)(?![a-z])/i.test(word3) && matches.indexOf(word1 + " " + word2) == -1){
              matches.push(word1+" "+word2);
        }else if (fnames.indexOf(word1) != -1 && !/(st|ln|lane|rd|road|dr|blv|cir|way|wy|pass)(?![a-z])/i.test(word2)  && matches.indexOf(word1) == -1){
              matches.push(word1);
        }
    }
    return matches;
}

我看不出有什么问题

var matches1 = findPeople("Brendon K. Smith", "brendon seth|smith gordon");
console.log(JSON.stringify(matches1)); // outputs ["brendon smith"] as expected
var matches2 = findPeople("Brendon Gordon", "brendon seth|smith gordon");
console.log(JSON.stringify(matches2)); // outputs ["brendon gordon"] as expected
var matches3 = findPeople("Seth", "brendon seth|smith gordon");
console.log(JSON.stringify(matches3)); // outputs ["seth"] as expected

function findPeople(text,nameList){
    text=text.replace(/[^a-z0-9\s]/gi, " ");
    text=text.replace(/\s+/gi," ");
    var spliText=text.split(/\s+/gi);
    nameList=nameList.split("|")
    var fnames=nameList[0].split(/\s/gi);
    var lnames=nameList[1].split(/\s/gi);
    console.log(fnames.slice(0,15),lnames.slice(0,15))

    var matches = [];
    for (var i=0;i<spliText.length;i++){
        var word1=spliText[i].toLowerCase();
        try{var word2=spliText[i+1].toLowerCase();}catch(e){word2="NOPE";}
        try{var word3=spliText[i+2].toLowerCase();}catch(e){word3="NOPE";}
        try{var word4=spliText[i+3].toLowerCase();}catch(e){word4="NOPE";}
        if (fnames.indexOf(word1) != -1 && lnames.indexOf(word3) != -1 && word1!=word3 && !/(st|ln|lane|rd|road|dr|blv|cir|way|wy|pass)(?![a-z])/i.test(word4) && matches.indexOf(word1 + " " + word3) == -1){
              matches.push(word1 + " " + word3);
        }else if (fnames.indexOf(word1) != -1 && lnames.indexOf(word2) != -1 && word1!=word2 && !/(st|ln|lane|rd|road|dr|blv|cir|way|wy|pass)(?![a-z])/i.test(word3) && matches.indexOf(word1 + " " + word2) == -1){
              matches.push(word1+" "+word2);
        }else if (fnames.indexOf(word1) != -1 && !/(st|ln|lane|rd|road|dr|blv|cir|way|wy|pass)(?![a-z])/i.test(word2)  && matches.indexOf(word1) == -1){
              matches.push(word1);
        }
    }
    return matches;
}

我终于解决了。由于某些原因,JSON没有返回隐藏字符/r,因此有一个隐藏的/r没有被拆分


谢谢你的帮助

为什么所有正则表达式上都有g标志?也发布你的数组。我的意思是,工作修复和非工作修复数组。g标志代表全局,我希望它匹配所有实例,而不仅仅是第一个。这些数组非常大,但你可以在我展示的图片中看到它们的一部分,这是前15个元素。在第一种情况下是这样的,我在/s上拆分,但第二种情况是不工作的,我在\n上拆分,因此任何\n字符都将被删除。但是,其中可能还有其他不可见的字符,因此我认为这可能是一个很好的猜测,这就是\s捕获并在其上执行额外拆分的原因。您知道是否有任何方法可以使js从命令提示符显示任何隐藏的字符吗?因此我使用JSON.stringify进行了检查,它返回的结果是那里没有不可见的字符。您是否担心输出显示lnames[0]==“smith”为false?@JasonTiemann我在答案中发布了一些代码,在JSFIDLE上运行此命令后,我看不出有任何问题。