I have following working code.
indexMatches = [];
function getMatchIndexes(str, toMatch) {
var toMatchLength = toMatch.length,
match,
i = 0;
while ((match = str.indexOf(toMatch, i)) > -1) {
indexMatches.push(toMatch);
i = match + toMatchLength;
}
return indexMatches;
}
console.log(getMatchIndexes("this is code [table which has [table table [row and rows [table]", "[table"));
Fiddle here: https://jsfiddle.net/vqqq1wj4/
However I have to match 2 string to search [table and [row and add to indexes. Currently it accepts only 1 parameter to search. I tried adding the same with OR operator in while but it doesnt work. Ideally I should write the following code
getMatchIndexes(str, "[table","[row");
and it would return the array below according to their index and positions properly.
[ "[table", "[table", "[row", "[table" ]