Skip to content

Regular expressions rules

Reports a regular expression whose complexity passes threshold. Quantifiers, lookarounds, alternatives, character classes, and backreferences add to the score, and nesting adds more.

On in the all config.

A long, nested regular expression is hard to read and to change without breaking a case.

Split the expression into smaller named parts, or check parts of the input with plain code.

A pattern that nests alternatives inside repeated groups three levels deep can pass 20 and report complex.

Option Type Default What it does
threshold Whole number 20 The highest complexity a regular expression may have.

Reports a regular expression whose top-level alternatives start with ^ only in the first alternative or end with $ only in the last, such as /^a|b|c$/. The pair /^\s+|\s+$/ for trimming passes.

On in the recommended and all configs.

An anchor binds only to its own alternative, so /^a|b$/ means ^a or b$, not ^(a|b)$.

Group the alternatives, such as /^(?:a|b)$/, or anchor each alternative on purpose.

/^error|warning$/ reports anchors. Write /^(?:error|warning)$/.

Reports a quantifier in a regular expression whose element can match the empty string, such as (a*)*, (?:a|)+, or (\b)*, in a literal or a RegExp call with a known pattern.

On in the recommended and all configs.

Repeating something that can match nothing gains nothing, and can make the engine backtrack for a long time.

Make the repeated element match at least one character, or remove the outer quantifier.

/(a*)*b/ reports empty. Write /a*b/.

Reports a lazy quantifier that only a quantifier able to match nothing follows at the end of a top-level alternative, such as a*? in /a*?b*/. regexp/no-lazy-ends reports a lazy quantifier that ends the pattern itself.

On in the recommended and all configs.

Nothing after the lazy quantifier forces it to match more, so it always matches its minimum.

Make the quantifier greedy, or make the part after it match at least one character.

/a*?b*/ reports lazy, because a*? always matches nothing.

Reports exec on a new global regular expression in the condition of a loop, and a global regular expression variable whose test or exec runs on different subjects without a reset of lastIndex.

On in the recommended and all configs.

A global regular expression remembers where its last match ended, so the next call on another input starts in the middle and can miss the match.

Remove the g flag when the expression only tests, reset lastIndex before each new input, or create the expression once outside the loop.

const word = /a/g; word.test(first); word.test(second); reports subjects.

Reports a string passed as the pattern of match, matchAll, or search on a string.

On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.

These methods compile a string argument as a regular expression, so . or ? in it change the match, and the regular expression rules cannot check a pattern hidden in a string.

Pass a regular expression literal, or new RegExp(pattern) when the pattern is built at run time.

title.match("v1.2") reports pattern. Write title.match(/v1\.2/u).

Reports a read of a named group, such as match.groups.name, on the result of exec, match, or matchAll when the regular expression has no capturing group of that name.

On in the recommended and all configs.

A missing group name reads undefined, usually because the name has a typo or the group was renamed.

Read a group name that the regular expression defines, or add the named group.

const date = /(?<year>\d+)/u.exec(text); date?.groups?.day; reports unknown.