即使匹配发生,Javascript数组也拒绝返回indexOf

时间:2017-03-11 14:45:00

标签: javascript arrays indexof

所以我一直有一个可以想象的最奇怪的问题,我有一个包含许多姓氏的javascript数组,它应该通过文本并通过循环显示它给出的单词并放置它们来挑选出名称在数组的indexOf中查看它是否为非负数,但是,我在首先拆分数组时犯了一个错误,我在regex \ s上拆分,并且每个常规字符串之间都有一个空字符串。 有趣的是,一个实际上工作,但当我修复它分裂\ n时,数组是相同的(减去空字符串),但现在它拒绝通过给定相同输入的indexOf匹配。我在控制台中设置了一个断点来自行检查,但是我发现了最奇怪的事情,我还没有发现(下图)。

The initial accident, which oddly works

The fix which somehow doesn't work

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

这里是参考代码

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

2 个答案:

答案 0 :(得分:-1)

我没有看到问题

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

答案 1 :(得分:-1)

我终于解决了它。出于某种原因,JSON没有返回隐藏的字符/ r,所以有一个隐藏的/ r没有被拆分。

感谢您的帮助!