Skip to content

no-regex-spaces

Reports two or more spaces outside a character class in the constant string given to new Regex(...) and the static methods of Regex.

Several spaces in a row are hard to count, and a pattern that has them is easily changed by an editor that collapses white space or by a reader who counts wrongly. The number of spaces is part of what the pattern matches.

To fix, write the number as a count (a {3}b).

Reported
class A {
void f(String s) {
...
Pattern.compile("a b");
...
Pattern.compile("a b");
Other.compile("a b");
...
csharp:
lint:
rules:
no-regex-spaces: warn # off (default), warn, error
java:
lint:
rules:
no-regex-spaces: warn # off (default), warn, error
js:
lint:
rules:
no-regex-spaces: warn # off (default), warn, error
php:
lint:
rules:
no-regex-spaces: warn # off (default), warn, error
python:
lint:
rules:
no-regex-spaces: warn # off (default), warn, error

Enabled by the presets: js/eslint-recommended, js/biome-recommended.

Suppress one line with sudsy-disable-next-line no-regex-spaces in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
eslint no-regex-spaces full