Regular expressions rules
max-regex-complexity
Section titled “max-regex-complexity”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.
What to do
Section titled “What to do”Split the expression into smaller named parts, or check parts of the input with plain code.
Example
Section titled “Example”A pattern that nests alternatives inside repeated groups three levels deep can pass 20 and report complex.
Options
Section titled “Options”| Option | Type | Default | What it does |
|---|---|---|---|
threshold |
Whole number | 20 |
The highest complexity a regular expression may have. |
no-ambiguous-anchors
Section titled “no-ambiguous-anchors”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)$.
What to do
Section titled “What to do”Group the alternatives, such as /^(?:a|b)$/, or anchor each alternative on purpose.
Example
Section titled “Example”/^error|warning$/ reports anchors. Write /^(?:error|warning)$/.
no-empty-repetition
Section titled “no-empty-repetition”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.
What to do
Section titled “What to do”Make the repeated element match at least one character, or remove the outer quantifier.
Example
Section titled “Example”/(a*)*b/ reports empty. Write /a*b/.
no-lazy-before-optional
Section titled “no-lazy-before-optional”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.
What to do
Section titled “What to do”Make the quantifier greedy, or make the part after it match at least one character.
Example
Section titled “Example”/a*?b*/ reports lazy, because a*? always matches nothing.
no-stateful-regex
Section titled “no-stateful-regex”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.
What to do
Section titled “What to do”Remove the g flag when the expression only tests, reset lastIndex before each new input, or create the expression once outside the loop.
Example
Section titled “Example”const word = /a/g; word.test(first); word.test(second); reports subjects.
no-string-regex-pattern
Section titled “no-string-regex-pattern”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.
What to do
Section titled “What to do”Pass a regular expression literal, or new RegExp(pattern) when the pattern is built at run time.
Example
Section titled “Example”title.match("v1.2") reports pattern. Write title.match(/v1\.2/u).
no-unknown-regex-group
Section titled “no-unknown-regex-group”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.
What to do
Section titled “What to do”Read a group name that the regular expression defines, or add the named group.
Example
Section titled “Example”const date = /(?<year>\d+)/u.exec(text); date?.groups?.day; reports unknown.