Rules that catch potential errors, bugs, and issues that could cause runtime failures or unexpected behavior in regular expressions. These 25 rules focus on preventing problematic patterns that might not work as intended.
All error prevention rules implement the standard ESLint rule interface:
interface RuleModule {
meta: RuleMeta;
create: (context: RuleContext) => RuleListener;
}
interface RuleMeta {
type: "problem";
docs: {
description: string;
category: "Possible Errors";
recommended: boolean;
url: string;
ruleName: string;
};
fixable?: "code" | "whitespace";
schema: JSONSchema4;
messages: Record<string, string>;
}
interface RuleContext {
getSourceCode(): SourceCode;
report(descriptor: ReportDescriptor): void;
options: any[];
settings: { [name: string]: any };
}Disallows confusing quantifiers that may not behave as expected.
// Rule: regexp/confusing-quantifier
// Type: problem
// Fixable: no
// Recommended: trueEnforces consistent escaping of control characters to prevent unintended behavior.
// Rule: regexp/control-character-escape
// Type: problem
// Fixable: code
// Recommended: trueEnforces consistent use of grapheme string literals to handle Unicode properly.
// Rule: regexp/grapheme-string-literal
// Type: problem
// Fixable: code
// Recommended: falseEnforces consistent hexadecimal escape sequences to prevent encoding issues.
// Rule: regexp/hexadecimal-escape
// Type: problem
// Fixable: code
// Recommended: falseEnforces consistent letter case in character classes to prevent case sensitivity bugs.
// Rule: regexp/letter-case
// Type: problem
// Fixable: code
// Recommended: falseEnforces consistent match-any syntax to prevent ambiguous patterns.
// Rule: regexp/match-any
// Type: problem
// Fixable: code
// Recommended: trueEnforces proper use of escapes on negation to prevent logical errors.
// Rule: regexp/negation
// Type: problem
// Fixable: code
// Recommended: trueDisallows elements that contradict assertions, which always fail.
// Rule: regexp/no-contradiction-with-assertion
// Type: problem
// Fixable: no
// Recommended: trueDisallows control characters that may cause unexpected behavior.
// Rule: regexp/no-control-character
// Type: problem
// Fixable: no
// Recommended: falseDisallows duplicate characters in character classes that are redundant.
// Rule: regexp/no-dupe-characters-character-class
// Type: problem
// Fixable: code
// Recommended: trueDisallows duplicate disjunctions that make patterns redundant.
// Rule: regexp/no-dupe-disjunctions
// Type: problem
// Fixable: no
// Recommended: trueDisallows alternatives without elements that may not work as expected.
// Rule: regexp/no-empty-alternative
// Type: problem
// Fixable: code
// Recommended: trueDisallows capturing groups that capture empty strings, often unintentional.
// Rule: regexp/no-empty-capturing-group
// Type: problem
// Fixable: no
// Recommended: trueDisallows character classes that match no characters, which always fail.
// Rule: regexp/no-empty-character-class
// Type: problem
// Fixable: no
// Recommended: trueDisallows empty groups that serve no purpose and may indicate errors.
// Rule: regexp/no-empty-group
// Type: problem
// Fixable: code
// Recommended: trueDisallows empty lookaround assertions that always succeed or fail.
// Rule: regexp/no-empty-lookarounds-assertion
// Type: problem
// Fixable: no
// Recommended: trueDisallows empty string literals in character classes.
// Rule: regexp/no-empty-string-literal
// Type: problem
// Fixable: code
// Recommended: trueDisallows escape backspace ([\b]) which is often confused with word boundary \b.
// Rule: regexp/no-escape-backspace
// Type: problem
// Fixable: no
// Recommended: trueDisallows unnecessary nested lookaround assertions that are redundant.
// Rule: regexp/no-extra-lookaround-assertions
// Type: problem
// Fixable: code
// Recommended: trueDisallows invalid regular expression strings in RegExp constructors.
// Rule: regexp/no-invalid-regexp
// Type: problem
// Fixable: no
// Recommended: trueDisallows invisible raw characters that may cause confusion.
// Rule: regexp/no-invisible-character
// Type: problem
// Fixable: code
// Recommended: falseDisallows lazy quantifiers at the end of expressions where they have no effect.
// Rule: regexp/no-lazy-ends
// Type: problem
// Fixable: code
// Recommended: trueDisallows legacy RegExp features that may not be supported across environments.
// Rule: regexp/no-legacy-features
// Type: problem
// Fixable: no
// Recommended: falseDisallows capturing groups that do not behave as expected.
// Rule: regexp/no-misleading-capturing-group
// Type: problem
// Fixable: no
// Recommended: trueDisallows multi-code-point characters in character classes that may not work as expected.
// Rule: regexp/no-misleading-unicode-character
// Type: problem
// Fixable: no
// Recommended: true// ❌ Confusing quantifier
/a{1,1}/ // Should be just /a/
// ❌ Empty character class
/[]/ // Will never match anything
// ❌ Duplicate characters
/[abbc]/ // The 'b' is duplicated
// ❌ Empty capturing group
/()/ // Captures empty string
// ❌ Contradictory assertion
/\b\B/ // Can never match (word boundary AND non-word boundary)// ✅ Clear quantifier
/a/
// ✅ Valid character class
/[abc]/
// ✅ No duplicate characters
/[abc]/
// ✅ Meaningful capturing group
/(abc)/
// ✅ Non-contradictory assertion
/\b/ // or /\B/ but not both