Skip to content

VBC-096: complex-regex-no-comment ​

warning

Category: clean-code
Analysis: Regex (line by line)
File types: .js, .ts, .tsx, .astro
Scope: undefined
Tags: clean-code

What it reports ​

Long regular expression literal at line {line} with no explanation. Add a comment stating what it matches, or build it from named parts.

Flagged ​

js
const re = /^[a-z0-9]+(?:[-_][a-z0-9]+)*@example\.com$/;
js
const text = html.replace(/<!--[\s\S]*?-->/g, ' ') // strips comments
  .replace(/<\/(p|div|li|h[1-6]|br|tr|td|th|blockquote|pre|ul|ol)>|<br\s*\/?>/gi, ' ');
js
// fetch the page
load();
const re = /^[a-z0-9]+(?:[-_][a-z0-9]+)*@example\.com$/;

Not flagged ​

js
const re = /^\d+$/;
js
// a slug: lowercase words joined by single hyphens or underscores, then the domain
const re = /^[a-z0-9]+(?:[-_][a-z0-9]+)*@example\.com$/;
js
/* the domain part of an address */

const re = /^[a-z0-9]+(?:[-_][a-z0-9]+)*@example\.com$/;
js
const re = /^[a-z0-9]+(?:[-_][a-z0-9]+)*@example\.com$/; // a slug at the domain
tsx
const Card = () => (
  <p>Is your code <span className="green">Engineering Substance</span> or <span className="red">Slop</span> today?</p>
);

Pattern ​

regex
/(?:[^/\n\\]|\\.){30,}/[dgimsuvy]*

Released under the MIT License.