Rules that enforce coding standards, performance optimizations, and maintainability improvements for regular expressions. These 25 rules focus on promoting efficient, readable, and maintainable regex patterns.
All best practice rules implement the standard ESLint rule interface:
interface RuleModule {
meta: RuleMeta;
create: (context: RuleContext) => RuleListener;
}
interface RuleMeta {
type: "suggestion";
docs: {
description: string;
category: "Best Practices";
recommended: boolean;
url: string;
ruleName: string;
};
fixable?: "code" | "whitespace";
schema: JSONSchema4;
messages: Record<string, string>;
}
interface RuleListener {
[key: string]: (node: ESTree.Node) => void;
}Disallows missing global flag in patterns where it's likely needed.
// Rule: regexp/no-missing-g-flag
// Type: suggestion
// Fixable: no
// Recommended: falseDisallows non-standard flags that may not be supported across environments.
// Rule: regexp/no-non-standard-flag
// Type: suggestion
// Fixable: no
// Recommended: falseDisallows unnecessary regex flags that have no effect.
// Rule: regexp/no-useless-flag
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows obscure character ranges that are hard to understand.
// Rule: regexp/no-obscure-range
// Type: suggestion
// Fixable: no
// Recommended: falseDisallows octal escape sequences which are deprecated.
// Rule: regexp/no-octal
// Type: suggestion
// Fixable: no
// Recommended: falseDisallows character classes containing only one character.
// Rule: regexp/no-useless-character-class
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows unnecessary character ranges.
// Rule: regexp/no-useless-range
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows unnecessary elements in expression character classes.
// Rule: regexp/no-useless-set-operand
// Type: suggestion
// Fixable: code
// Recommended: falseDisallows optional assertions that have no effect.
// Rule: regexp/no-optional-assertion
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows trivially nested assertions that can be simplified.
// Rule: regexp/no-trivially-nested-assertion
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows assertions that are known to always accept or reject.
// Rule: regexp/no-useless-assertions
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows backreferences that reference a group that might not be matched.
// Rule: regexp/no-potentially-useless-backreference
// Type: suggestion
// Fixable: no
// Recommended: trueDisallows unused capturing groups that waste memory and processing.
// Rule: regexp/no-unused-capturing-group
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows useless backreferences that don't add value.
// Rule: regexp/no-useless-backreference
// Type: suggestion
// Fixable: no
// Recommended: trueDisallows useless non-capturing groups that can be removed.
// Rule: regexp/no-useless-non-capturing-group
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows nested quantifiers that can be rewritten as one.
// Rule: regexp/no-trivially-nested-quantifier
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows unnecessarily non-greedy quantifiers.
// Rule: regexp/no-useless-lazy
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows quantifiers that can be removed without changing behavior.
// Rule: regexp/no-useless-quantifier
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows unnecessary {n,m} quantifiers that can be simplified.
// Rule: regexp/no-useless-two-nums-quantifier
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows exponential and polynomial backtracking that causes performance issues.
// Rule: regexp/no-super-linear-backtracking
// Type: suggestion
// Fixable: no
// Recommended: trueDisallows quantifiers that cause quadratic moves and performance problems.
// Rule: regexp/no-super-linear-move
// Type: suggestion
// Fixable: no
// Recommended: falseDisallows standalone backslashes that may cause errors.
// Rule: regexp/no-standalone-backslash
// Type: suggestion
// Fixable: no
// Recommended: trueDisallows useless $ replacements in string replacements.
// Rule: regexp/no-useless-dollar-replacements
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows useless escape sequences that don't need escaping.
// Rule: regexp/no-useless-escape
// Type: suggestion
// Fixable: code
// Recommended: trueDisallows string disjunctions of single characters that can be character classes.
// Rule: regexp/no-useless-string-literal
// Type: suggestion
// Fixable: code
// Recommended: false// ❌ Super-linear backtracking (catastrophic backtracking)
/(a+)+b/ // Can cause exponential time complexity
// ❌ Useless quantifier
/a{1}/ // Same as just /a/
// ❌ Unnecessary non-greedy
/a*?$/ // Non-greedy has no effect at end
// ✅ Optimized patterns
/a+b/ // Direct match, no nested quantifiers
/a/ // Simple quantifier
/a*$/ // Greedy is fine at end// ❌ Unused capturing group
/(\d+)-\d+/.exec("123-456")[0] // Only using full match
// ❌ Useless non-capturing group
/(?:a)/ // Just use /a/
// ✅ Optimized groups
/\d+-\d+/.exec("123-456")[0] // No capturing group needed
/a/ // No grouping needed// ❌ Single character in class
/[a]/ // Same as just /a/
// ❌ Useless range
/[a-a]/ // Same as just /a/
// ❌ String disjunction of single chars
/a|b|c/ // Can be character class
// ✅ Optimized character classes
/a/ // Direct character
/a/ // Direct character
/[abc]/ // Proper character class// ❌ Useless global flag
/abc/g.test(str) // test() only finds first match anyway
// ❌ Missing global flag
str.match(/\d+/) // Only finds first number, likely want all
// ✅ Proper flag usage
/abc/.test(str) // No global needed for test
str.match(/\d+/g) // Global flag for finding all matches