0

请记住,这还没有完成,我唯一的问题是为什么 console.log 会产生这个输出?

/>B /* 这是我所期望的 */

/>BD /* 我期望的第二个输出只是 ">/D" 我对它是如何提出的感到困惑 >/"BD" */

graphArray = ["4","A","B","C","D","A-B","B-D","B-C","C-D"];
pointsArray = [];
linesArray = [];
nodes = graphArray[0];
for (i = 1; i < graphArray.length; i++) {
    if (i <= nodes) {
    pointsArray.push(graphArray[i]);
    }
    if (i > nodes) {
        linesArray.push(graphArray[i]);
    }
}
nextpoint = pointsArray[0];
patt = new RegExp(/-.*/);
patt2 = new RegExp(nextpoint + "-");
for (i = 0; i < linesArray.length; i++) {
    x = 0;
    while (x < linesArray.length) {
        if (linesArray[x].replace(patt,"") === nextpoint) {
            nextpoint = linesArray[x].replace(patt2,"");
            console.log(nextpoint);
        }
        x++;
    }
}

编辑:敲打额头对我来说一定为时已晚,我不敢相信我错过了。谢谢你指出这一点。解决了。

4

1 回答 1

1

patt2 = new RegExp(nextpoint + "-");应该在循环内

for (i = 0; i < linesArray.length; i++) {
    x = 0;
    while (x < linesArray.length) {

        patt2 = new RegExp(nextpoint + "-");
        if (linesArray[x].replace(patt,"") === nextpoint) {
            nextpoint = linesArray[x].replace(patt2,"");
            console.log(nextpoint);
        }
        x++;
    }
}
于 2013-11-05T06:32:02.213 回答