The second parameter to indexOf specifies the starting point of the search. You can use a recursive call to a function that searches the string from the next starting point:
Code:
function getIndex(searchMe, searchFor, count, startFrom) {
//Search the string, if startFrom is undefined, use 0 as default
var index = searchMe.indexOf(searchFor, startFrom ? startFrom : 0);
//If count > 1, call this function again starting at index+1
return (count > 1) ? getIndex(searchMe, searchFor, count-1, index+1) : index;
}
//Try it out:
var teststring = "testtesttest";
var findstring = "test";
//Get the 1st instance, returns 0 (the index of the 1st character)
trace(getIndex(teststring, findstring, 1));
//Get the 2nd instance, returns 4
trace(getIndex(teststring, findstring, 2));
//Get the 3rd instance, returns 8
trace(getIndex(teststring, findstring, 3));
//Get the 4th instance, returns -1 (not found)
trace(getIndex(teststring, findstring, 4));