regex-last-index
test or exec on a regex with the g or y flag, called again with the lastIndex the last call left: in a loop over other strings, in a method called again, in a callback. On by default, as an error.
Why: with g or y, a regex remembers where its last match ended (lastIndex), and test and exec start there, not at the start of the string they are given. That is what walks the matches of one string. Given another string, or the same one again, the search starts in the middle: a match is missed, and test answers true, false, true for one text. TypeScript types lastIndex as a number like any other, so nothing says so.
❌ Rejected: the second call starts where the first match ended
✕ Rejectedregex-last-index
ts
export class Finder {
private readonly digits = /\d+/g;
has(text: string): boolean {
return this.digits.test(text);
}
}✅ Accepted: without the flag, each test searches the whole string
✓ Accepted0 diagnostics
ts
export class Finder {
private readonly digits = /\d+/;
has(text: string): boolean {
return this.digits.test(text);
}
}The quick fix Drop the g flag: each test starts at the beginning does that, where test is every use of the regex.
❌ Rejected: each line is searched from where the match in the previous one ended
✕ Rejectedregex-last-index
ts
export function numbered(lines: readonly string[]): number {
const digits = /\d+/g;
let count = 0;
for (const line of lines) if (digits.test(line)) count += 1;
return count;
}✅ Accepted: the matches of one string, walked by matchAll, which doesn't use lastIndex
✓ Accepted0 diagnostics
ts
export function numbers(text: string): readonly string[] {
return [...text.matchAll(/\d+/g)].map((m) => m[0]);
}
export function numbered(lines: readonly string[]): number {
return lines.filter((line) => /\d+/.test(line)).length;
}ts
export function numbers(text: string): readonly string[] {
{ const __la1 = [...text.matchAll(/\d+/g)]; const __lr1: any[] = new Array(__la1.length); for (let __li1 = 0, __ln1 = __la1.length; __li1 < __ln1; __li1++) { if (!(__li1 in __la1)) continue; const __le1 = __la1[__li1]!; { let m = __le1; __lr1[__li1] = (m[0]); }} return __lr1; }
}
export function numbered(lines: readonly string[]): number {
return lines.filter((line) => /\d+/.test(line)).length;
}What it reports
A regex is known to have the flag when it is a literal (/x/g, /x/y) or new RegExp(x, "g") / RegExp(x, "g") with the flags written, kept in a const or let of a function, a parameter's default (re = /x/g), or a class field (re = /x/g, or this.re = /x/g in the constructor). Reported, where nothing sets its lastIndex in between (re.lastIndex = 0):
- a
test/execin a loop below where the regex is made, on a value the loop changes (afor...ofelement, an index into a list); - one in a callback (a function written in the function that makes the regex), on a value the callback is given;
- a field's in a method, which is called again (not in the constructor);
- a second call in straight-line code on another string than the first.
Not reported:
- the idiom: a loop over one string, walking its matches (
while ((m = re.exec(text)) !== null),let m = re.exec(text); while (m !== null) { ...; m = re.exec(text); }). Run to its end, the last call found nothing and putlastIndexback to0, so what runs the loop again starts afresh; a loop left early (return,break) is judged by what runs it again; - a callback or method that reads one string each time (a tokenizer's
next()over its input,this.text): it walks that string's matches; - a function whose last use of the regex is a loop it decides and runs to its end;
- a regex made where it is used (
/x/g.test(s), or aconstin the loop's body); matchAll,replace,replaceAll,split,matchandsearch, which don't start atlastIndex;- a module-level regex:
no-mutable-globalreports it.
Quick fixes
- Drop the
gflag: each test starts at the beginning, on a literal withoutywhose every use istest(atestfinds the first match without the flag). Withexec, the flag may be what walks a string's matches, so the fix isn't offered: setlastIndexor usematchAll.
What the rule can't see
- A regex passed in: a parameter without a default, a value returned by a call, a field set outside the constructor: its flags aren't known where it is used.
- Flags not written:
new RegExp(source, flags)withflagsa variable. - A reset elsewhere:
lastIndexset by a function the code calls, which the rule doesn't follow; and one set in another branch than the call's, which it takes as setting it. - A loop left early that nothing runs again: not reported, as nothing starts from the leftover
lastIndex.
Related: no-mutable-global; guide: Built-ins.