I am trying to get the index of a matched string from inside a for loop of values to match the string with...
Example:
var arr = ["foo", "bar"],
arr2 = [];
for(var j=0;j<arr.length;j++){
var str = "foo bar flub alien alcohol foo bar foo",
re = new RegExp("("+arr[j]+")", "ig"),
//method = str.match(re) || re.exec(str),
obj = {match: method, index: method.index};
arr2.push(obj);
console.log(obj);
}
I am aware this currently doesn't work and I'm trying to work out which method to go with for this problem. Getting stuck every time, I have come to the Stack Overflow community to ask you, what am I doing wrong when I am trying to achieve the following (for the result of arr2):
[{string: "foo", index: 0},
{string: "foo", index: 27},
{string: "bar", index: 4},
{string: "bar", index: 30}]