I can't find out a solution to the following problem and it's driving me nuts. I need to find every position of a string within another string.
This is what I have come up with:
function getMatchIndices(regex, str) {
let result = [];
let match;
let regex = new RegExp(regex, 'g');
while (match = regex.exec(str))
result.push(match.index);
return result;
}
const line = "aabaabbaababaababaaabaabaaabaabbabaababa";
const rule = 'aba';
const indices = getMatchIndices(new RegExp(rule, "g"), line);
console.log(indices);
Now, the issue is that this does NOT match aba's that are formed in the middle of two other matches...
Here is an image illustrating the problem:
Any ideas?