fix: return last found index even if fewer than n instances found, instead of undefined

This commit is contained in:
Aly Thobani 2024-04-27 10:59:24 -07:00
parent 7ca0e78ebc
commit 5215915014

View file

@ -47,12 +47,13 @@ function getNthNextInstanceOfPattern({
}): number {
const globalRegex = makeGlobalRegex(regex);
globalRegex.lastIndex = startingIdx + 1;
let currMatch;
let currMatch, lastMatch;
let numMatchesFound = 0;
while (numMatchesFound < n && (currMatch = globalRegex.exec(content)) != null) {
lastMatch = currMatch;
numMatchesFound++;
}
return currMatch?.index;
return lastMatch?.index;
}
/**