let str = 'As sly as a fox, as strong as an ox';
let target = 'as'; // 这是我们要查找的目标
let pos = 0;
while (true) {
let foundPos = str.indexOf(target, pos);
if (foundPos == -1) break;
alert( `Found at ${foundPos}` );
pos = foundPos + 1; // 继续从下一个位置查找
}
有个疑问是,这里的
while (true) {
...
...
}
感觉应该是个无限循环,但实际不是,为什么呢?
麻烦高手给解释下,谢谢~
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…