oxc-project/oxc · warning · OxcDiagnostic

Multiple consecutive spaces are hard to count.

Error message

Multiple consecutive spaces are hard to count.

What it means

oxlint's port of ESLint `no-regex-spaces`. A regex containing two or more consecutive literal space characters is flagged because the human eye cannot reliably count the spaces. The help text suggests the fix directly: replace the run with one space plus an explicit quantifier (` {n}`), and the rule provides an autofix that rewrites the run in place.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_regex_spaces.rs:23

};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_regular_expression::{
    ConstructorParser, Options,
    ast::{Character, Pattern},
    visit::{RegExpAstKind, Visit},
};
use oxc_span::Span;

use crate::{
    AstNode,
    context::LintContext,
    fixer::{RuleFix, RuleFixer},
    rule::Rule,
};

fn no_regex_spaces_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Multiple consecutive spaces are hard to count.")
        .with_help(format!("Use a quantifier: ` {{{size}}}`", size = span.size()))
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoRegexSpaces;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow 2+ consecutive spaces in regular expressions.
    ///
    /// ### Why is this bad?
    ///
    /// In a regular expression, it is hard to tell how many spaces are
    /// intended to be matched. It is better to use only one space and
    /// then specify how many spaces are expected using a quantifier.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace the run of N spaces with a single space and a quantifier: `/ {3}/` instead of `/ /`.
  2. Run `oxlint --fix` to apply the rule's autofix automatically.
  3. Use `\s{3}` or `[ ]{3}` if your toolchain collapses literal space runs.

Example fix

// before
const re = /foo   bar/;

// after
const re = /foo {3}bar/;
Defensive patterns

Strategy: validation

Validate before calling

// Flag regex sources with 2+ consecutive spaces before they reach review
function hasUnquantifiedSpaces(reSource) {
  return / {2,}(?!\})/.test(reSource);
}
module.exports = { hasUnquantifiedSpaces };

Prevention

When it happens

Trigger: Any regex literal or `new RegExp("...")` source containing 2+ consecutive spaces: `/foo bar/`, `/a b/`, `new RegExp("x y")`. The diagnostic span size equals the length of the space run, which feeds the suggested ` {size}` quantifier.

Common situations: Patterns for parsing fixed-width log columns or ASCII tables; copy-pasting a pattern whose `{2}` quantifier was lost; editors that trim trailing whitespace silently changing the pattern's meaning.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/386212b6e9d11548. Report an issue: GitHub.