Rules that enforce consistent formatting, naming conventions, and stylistic choices for regular expressions to improve code readability and maintainability. These 32 rules focus on code style and consistency rather than correctness.
All stylistic rules implement the standard ESLint rule interface:
interface RuleModule {
meta: RuleMeta;
create: (context: RuleContext) => RuleListener;
}
interface RuleMeta {
type: "layout";
docs: {
description: string;
category: "Stylistic Issues";
recommended: boolean;
url: string;
ruleName: string;
};
fixable?: "code" | "whitespace";
schema: JSONSchema4;
messages: Record<string, string>;
}Disallows quantifiers with a maximum of zero, which never match.
// Rule: regexp/no-zero-quantifier
// Type: layout
// Fixable: no
// Recommended: falsePrefer + quantifier over {1,} for better readability.
// Rule: regexp/prefer-plus-quantifier
// Type: layout
// Fixable: code
// Recommended: falsePrefer ? quantifier over {0,1} for better readability.
// Rule: regexp/prefer-question-quantifier
// Type: layout
// Fixable: code
// Recommended: falsePrefer * quantifier over {0,} for better readability.
// Rule: regexp/prefer-star-quantifier
// Type: layout
// Fixable: code
// Recommended: falseEnforces using quantifier shorthand when possible.
// Rule: regexp/prefer-quantifier
// Type: layout
// Fixable: code
// Recommended: falseRequires optimal quantifiers for concatenated quantifiers.
// Rule: regexp/optimal-quantifier-concatenation
// Type: layout
// Fixable: code
// Recommended: falseDisallows alternatives of lookarounds that end with non-constant quantifier.
// Rule: regexp/optimal-lookaround-quantifier
// Type: layout
// Fixable: no
// Recommended: falsePrefer character class over alternatives for single characters.
// Rule: regexp/prefer-character-class
// Type: layout
// Fixable: code
// Recommended: falsePrefer \d over [0-9] for digit matching.
// Rule: regexp/prefer-d
// Type: layout
// Fixable: code
// Recommended: falsePrefer \w over [a-zA-Z0-9_] for word character matching.
// Rule: regexp/prefer-w
// Type: layout
// Fixable: code
// Recommended: falseEnforces using character class ranges when appropriate.
// Rule: regexp/prefer-range
// Type: layout
// Fixable: code
// Recommended: falsePrefer set operations over character classes when available.
// Rule: regexp/prefer-set-operation
// Type: layout
// Fixable: code
// Recommended: falseSimplifies set operations to their most readable form.
// Rule: regexp/simplify-set-operations
// Type: layout
// Fixable: code
// Recommended: falsePrefer named backreferences over numbered ones for clarity.
// Rule: regexp/prefer-named-backreference
// Type: layout
// Fixable: no
// Recommended: falsePrefer named capture groups over anonymous ones for maintainability.
// Rule: regexp/prefer-named-capture-group
// Type: layout
// Fixable: no
// Recommended: falsePrefer named replacements in replacement strings for clarity.
// Rule: regexp/prefer-named-replacement
// Type: layout
// Fixable: code
// Recommended: falsePrefer result array groups property over indexed access.
// Rule: regexp/prefer-result-array-groups
// Type: layout
// Fixable: code
// Recommended: falsePrefer lookaround over negated character class when applicable.
// Rule: regexp/prefer-lookaround
// Type: layout
// Fixable: code
// Recommended: falsePrefer predefined assertions over equivalent lookarounds.
// Rule: regexp/prefer-predefined-assertion
// Type: layout
// Fixable: code
// Recommended: falsePrefer Unicode codepoint escapes over other Unicode escape forms.
// Rule: regexp/prefer-unicode-codepoint-escapes
// Type: layout
// Fixable: code
// Recommended: falseEnforces consistent Unicode escape style.
// Rule: regexp/unicode-escape
// Type: layout
// Fixable: code
// Recommended: falseEnforces consistent Unicode property escape style.
// Rule: regexp/unicode-property
// Type: layout
// Fixable: code
// Recommended: falseEnforces escape of replacement $ character for consistency.
// Rule: regexp/prefer-escape-replacement-dollar-char
// Type: layout
// Fixable: code
// Recommended: falseEnforces that RegExp#exec is used instead of String#match when appropriate.
// Rule: regexp/prefer-regexp-exec
// Type: layout
// Fixable: no
// Recommended: falseEnforces that RegExp#test is used instead of String#match for boolean checks.
// Rule: regexp/prefer-regexp-test
// Type: layout
// Fixable: code
// Recommended: falseRequires Unicode flag for proper Unicode handling.
// Rule: regexp/require-unicode-regexp
// Type: layout
// Fixable: no
// Recommended: falseRequires Unicode sets flag when using set operations.
// Rule: regexp/require-unicode-sets-regexp
// Type: layout
// Fixable: no
// Recommended: falseUse ignore case flag instead of case-insensitive alternatives.
// Rule: regexp/use-ignore-case
// Type: layout
// Fixable: code
// Recommended: falseSorts alternatives in disjunctions for consistency.
// Rule: regexp/sort-alternatives
// Type: layout
// Fixable: code
// Recommended: falseSorts character class elements for consistency.
// Rule: regexp/sort-character-class-elements
// Type: layout
// Fixable: code
// Recommended: falseSorts flags in consistent order.
// Rule: regexp/sort-flags
// Type: layout
// Fixable: code
// Recommended: falseDisallows patterns that are not strictly valid regular expressions.
// Rule: regexp/strict
// Type: layout
// Fixable: no
// Recommended: false// ❌ Verbose quantifiers
/a{1,}/ // Could be a+
/a{0,1}/ // Could be a?
/a{0,}/ // Could be a*
// ✅ Preferred quantifier style
/a+/ // Clear and concise
/a?/ // Clear and concise
/a*/ // Clear and concise// ❌ Verbose character classes
/[0-9]/ // Could be \d
/[a-zA-Z0-9_]/ // Could be \w
/a|b|c/ // Could be character class
// ✅ Preferred character class style
/\d/ // Concise digit class
/\w/ // Concise word class
/[abc]/ // Proper character class// ❌ Anonymous capture groups and numeric references
const regex = /(\d{4})-(\d{2})-(\d{2})/;
const match = "2023-12-25".match(regex);
const year = match[1]; // Not clear what [1] represents
// ✅ Named capture groups and named references
const regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = "2023-12-25".match(regex);
const year = match.groups.year; // Clear what year represents// ❌ Using String#match for boolean checks
if ("test".match(/t/)) { }
// ❌ Using String#match when you need exec behavior
const match = "test".match(/t/);
// ✅ Using appropriate methods
if (/t/.test("test")) { } // Better for boolean checks
const match = /t/.exec("test"); // Better when you need match details// ❌ Inconsistent Unicode handling
/café/ // May not handle accents properly
/[\u0041-\u005A]/ // Verbose Unicode escapes
// ✅ Consistent Unicode style
/café/u // Proper Unicode handling
/[A-Z]/u // Clear range with Unicode flag
/\p{L}/u // Unicode property for letters