ESLint Plugin RegExp provides pre-configured rule sets for different use cases, supporting both legacy ESLint configuration and modern flat configuration formats.
The recommended configuration enables a curated set of rules that catch common errors and enforce best practices without being overly strict.
interface RecommendedConfig {
plugins: string[];
rules: Record<string, SeverityString>;
}
interface FlatRecommendedConfig {
plugins: { regexp: typeof plugin };
rules: Record<string, SeverityString>;
}Legacy ESLint configuration:
module.exports = {
extends: ["plugin:regexp/recommended"],
plugins: ["regexp"]
};Flat ESLint configuration:
import regexp from "eslint-plugin-regexp";
export default [
{
...regexp.configs["flat/recommended"]
}
];The all configuration enables every available rule in the plugin. Use with caution as it may be overly strict for some codebases.
interface AllConfig {
plugins: string[];
rules: Record<string, SeverityString>;
}
interface FlatAllConfig {
plugins: { regexp: typeof plugin };
rules: Record<string, SeverityString>;
}Legacy ESLint configuration:
module.exports = {
extends: ["plugin:regexp/all"],
plugins: ["regexp"]
};Flat ESLint configuration:
import regexp from "eslint-plugin-regexp";
export default [
{
...regexp.configs["flat/all"]
}
];type SeverityString = "error" | "warn" | "off";Rules can be configured with different severity levels:
"error": Rule violations will cause ESLint to exit with code 1"warn": Rule violations will be reported but won't cause ESLint to exit with non-zero code"off": Rule is disabledIndividual rules can be configured with options:
interface RuleConfig {
[ruleName: string]: SeverityString | [SeverityString, ...any[]];
}Examples:
{
"rules": {
"regexp/no-unused-capturing-group": "error",
"regexp/confusing-quantifier": "warn",
"regexp/letter-case": ["error", { "caseInsensitive": "ignore" }]
}
}The plugin configurations also modify some core ESLint rules to avoid conflicts:
The following core ESLint rules are disabled because the plugin provides better alternatives:
interface CoreRuleOverrides {
"no-invalid-regexp": "off";
"no-useless-backreference": "off";
"no-empty-character-class": "off";
}These core ESLint rules are kept enabled and work alongside plugin rules:
interface EnhancedCoreRules {
"no-control-regex": "error";
"no-misleading-character-class": "error";
"no-regex-spaces": "error";
"prefer-regex-literals": "error";
}You can create a custom configuration by extending the recommended config and overriding specific rules:
// Legacy format
module.exports = {
extends: ["plugin:regexp/recommended"],
plugins: ["regexp"],
rules: {
// Override recommended settings
"regexp/confusing-quantifier": "error",
"regexp/letter-case": "off",
// Add additional rules not in recommended
"regexp/prefer-lookaround": "warn"
}
};
// Flat format
import regexp from "eslint-plugin-regexp";
export default [
{
...regexp.configs["flat/recommended"],
rules: {
...regexp.configs["flat/recommended"].rules,
"regexp/confusing-quantifier": "error",
"regexp/letter-case": "off",
"regexp/prefer-lookaround": "warn"
}
}
];The plugin supports settings that can be configured to customize behavior:
interface RegexpSettings {
allowedCharacterRanges?: string | readonly string[];
}Configure plugin settings in your ESLint configuration:
// Legacy format
module.exports = {
extends: ["plugin:regexp/recommended"],
plugins: ["regexp"],
settings: {
regexp: {
allowedCharacterRanges: ["a-z", "A-Z", "0-9"]
}
}
};
// Flat format
export default [
{
...regexp.configs["flat/recommended"],
settings: {
regexp: {
allowedCharacterRanges: ["a-z", "A-Z", "0-9"]
}
}
}
];Configure rules differently for specific file patterns:
// Flat format only
export default [
{
...regexp.configs["flat/recommended"]
},
{
files: ["**/*.test.js"],
rules: {
// More lenient rules for test files
"regexp/confusing-quantifier": "off",
"regexp/prefer-escape": "off"
}
}
];