no-regex-spaces
Added in v0.5.0Configuration
Rule Details
Disallow multiple spaces in regular expressions. Two or more consecutive space
characters are hard to count by eye; an explicit {n} quantifier expresses the
same pattern unambiguously. The rule applies to both regex literals and the
RegExp / new RegExp constructors (skipped when RegExp is shadowed in the
enclosing scope or when the flags argument cannot be statically determined).
Consecutive spaces inside character classes ([...]) are intentionally allowed
and not reported.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Autofix
When the parsed pattern and the raw source text agree (typically any regex
literal, and RegExp / new RegExp calls whose pattern string contains no
escape sequences), the rule rewrites into {n}. When the pattern
contains escape sequences that differ from the raw source (e.g.
new RegExp('\\d ')), the rule reports but does not autofix — the index into
the parsed pattern would not map cleanly back to source positions.
Differences from ESLint
- Constructor patterns using capture names or Unicode properties newer than the bundled TypeScript parser's Unicode data can be skipped.
- For a constructor pattern with a negated
vclass where a range is followed by a string-valued operand, rslint skips a report that ESLint may emit because JavaScript rejects the pattern.