I am facing a bit problem in Regex
. I want to find the start and end index of the complete matched string in the input string.
e.g. I have an array of strings like
["a", "aa"]
and I have a text like I like a problem aa
I am doing with iteration of array strings.
let arr = ["a", "aa"];
let str = "I like a problem aa";
let indicesArr = [];
arr.forEach(a=>{
const regexObj = new RegExp(a, "gi");
let match;
while ((match = regexObj.exec(str))) {
let obj = { start: match.index, end: regexObj.lastIndex }
indicesArr.push(obj);
if(!match.index || !regexObj.lastIndex) break;
}
});
above code gives me the result
[
{start: 7, end: 8},
{start: 17, end: 18},
{start: 18, end: 19},
{start: 17, end: 19}
]
I want the result should be
[
{start: 7, end: 8},
{start: 17, end: 19}
]
Any suggestion would be very helpful, thanks:)