Dot or Newline Alternation

Identifier: regex.lint.alternation.dotNewline

When it triggers: An alternation reads (.|\n), the anti-pattern for any character including newlines. The dot already matches every character but a newline, so the branch adds nothing but a slower, two-way choice.

It stays silent in a pattern where a call runs a repeat whose count may vary, a+ or a{2,3} but not a{2} nor a repeated call, in the group it calls or anywhere when the pattern calls itself whole: PCRE2 compiles such a pattern by how all of it is written, and its JIT then misses matches its interpreter finds. /(a++(?:.|\n))c(?1)d/ finds abcabd in abcabcabd with the JIT, where /(a++[\s\S])c(?1)d/ finds nothing, from PCRE2 10.39 to 10.49.

Example (real regex lint output):

      → /(.|\n)/
    WARN Alternation (.|\n) is an anti-pattern for matching any character including newlines.
         ↳ Use the "s" (PCRE_DOTALL) flag to make "." match newlines, or use [\s\S] instead of (.|\n).

Fix: Use the s flag so the dot crosses newlines, or write [\s\S].


Try it. Open /(.|\n)/ in the playground: the finding is in the Diagnostics tab.

Edit on GitHub Feedback