2011-07-26 73 views

回答

4

可以使用indexOf方法的第二個參數來實現你想要的:

var str = "food", 
    index1 = str.indexOf("o"), 
    index2 = str.indexOf("o", index1+1); 
+0

啊我明白了!非常感謝你 – Wenn

12

我認爲對於非串做到這一點的最好辦法瑣碎的長度是RegExp.exec() function

var str = "Foooooooood!", 
    re = /o/g, 
    match; 
while (match = re.exec(str)) { 
    console.log(match.index); // logs 1 through 9 
}