Package detail

eslint-plugin-regexp

ota-meshi2.6mMIT2.7.0

ESLint plugin for finding RegExp mistakes and RegExp style guide violations.

eslint, eslintplugin, eslint-plugin, regexp

readme

Introduction

eslint-plugin-regexp is ESLint plugin for finding RegExp mistakes and RegExp style guide violations.

NPM license NPM version NPM downloads NPM downloads NPM downloads NPM downloads NPM downloads Build Status Coverage Status

:name_badge: Features

This ESLint plugin provides linting rules relate to better ways to help you avoid problems when using RegExp.

  • Find the wrong usage of regular expressions, and their hints.
  • Enforces a consistent style of regular expressions.
  • Find hints for writing optimized regular expressions.
  • 80 plugin rules for regular expression syntax and features.

You can check on the Online DEMO.

:book: Documentation

See documents.

:cd: Installation

npm install --save-dev eslint eslint-plugin-regexp

Requirements

  • ESLint v8.44.0 and above
  • Node.js v18.x, v20.x and above

:book: Usage

Add regexp to the plugins section of your eslint.config.js or .eslintrc configuration file (you can omit the eslint-plugin- prefix) and either use one of the two configurations available (recommended or all) or configure the rules you want:

The recommended configuration (New Config)

The plugin.configs["flat/recommended"] config enables a subset of the rules that should be most useful to most users. See lib/configs/rules/recommended.ts for more details.

// eslint.config.js
import * as regexpPlugin from "eslint-plugin-regexp"

export default [
    regexpPlugin.configs["flat/recommended"],
];

The recommended configuration (Legacy Config)

The plugin:regexp/recommended config enables a subset of the rules that should be most useful to most users. See lib/configs/rules/recommended.ts for more details.

// .eslintrc.js
module.exports = {
    "plugins": [
        "regexp"
    ],
    "extends": [
         // add more generic rulesets here, such as:
         // 'eslint:recommended',
        "plugin:regexp/recommended"
    ]
}

Advanced Configuration

Override/add specific rules configurations. See also: http://eslint.org/docs/user-guide/configuring.

// eslint.config.js
import * as regexpPlugin from "eslint-plugin-regexp"

export default [
    {
        plugins: { regexp: regexpPlugin },
        rules: {
            // Override/add rules settings here, such as:
            "regexp/rule-name": "error"
        }
    }
];
// .eslintrc.js
module.exports = {
    "plugins": [
        "regexp"
    ],
    "rules": {
        // Override/add rules settings here, such as:
        "regexp/rule-name": "error"
    }
}

Using the all configuration

The plugin.configs["flat/all"] / plugin:regexp/all config enables all rules. It's meant for testing, not for production use because it changes with every minor and major version of the plugin. Use it at your own risk. See lib/configs/rules/all.ts for more details.

:white_check_mark: Rules

💼 Configurations enabled in.\ ⚠️ Configurations set to warn in.\ 🟢 Set in the flat/recommended configuration.\ 🔵 Set in the recommended configuration.\ 🔧 Automatically fixable by the --fix CLI option.\ 💡 Manually fixable by editor suggestions.

Possible Errors

Name Description 💼 ⚠️ 🔧 💡
no-contradiction-with-assertion disallow elements that contradict assertions 🟢 🔵 💡
no-control-character disallow control characters 💡
no-dupe-disjunctions disallow duplicate disjunctions 🟢 🔵 💡
no-empty-alternative disallow alternatives without elements 🟢 🔵 💡
no-empty-capturing-group disallow capturing group that captures empty. 🟢 🔵
no-empty-character-class disallow character classes that match no characters 🟢 🔵
no-empty-group disallow empty group 🟢 🔵
no-empty-lookarounds-assertion disallow empty lookahead assertion or empty lookbehind assertion 🟢 🔵
no-escape-backspace disallow escape backspace ([\b]) 🟢 🔵 💡
no-invalid-regexp disallow invalid regular expression strings in RegExp constructors 🟢 🔵
no-lazy-ends disallow lazy quantifiers at the end of an expression 🟢 🔵 💡
no-misleading-capturing-group disallow capturing groups that do not behave as one would expect 🟢 🔵 💡
no-misleading-unicode-character disallow multi-code-point characters in character classes and quantifiers 🟢 🔵 🔧 💡
no-missing-g-flag disallow missing g flag in patterns used in String#matchAll and String#replaceAll 🟢 🔵 🔧
no-optional-assertion disallow optional assertions 🟢 🔵
no-potentially-useless-backreference disallow backreferences that reference a group that might not be matched 🟢 🔵
no-super-linear-backtracking disallow exponential and polynomial backtracking 🟢 🔵 🔧
no-super-linear-move disallow quantifiers that cause quadratic moves
no-useless-assertions disallow assertions that are known to always accept (or reject) 🟢 🔵 💡
no-useless-backreference disallow useless backreferences in regular expressions 🟢 🔵
no-useless-dollar-replacements disallow useless $ replacements in replacement string 🟢 🔵
strict disallow not strictly valid regular expressions 🟢 🔵 🔧 💡

Best Practices

Name Description 💼 ⚠️ 🔧 💡
confusing-quantifier disallow confusing quantifiers 🟢 🔵
control-character-escape enforce consistent escaping of control characters 🟢 🔵 🔧
negation enforce use of escapes on negation 🟢 🔵 🔧
no-dupe-characters-character-class disallow duplicate characters in the RegExp character class 🟢 🔵 🔧
no-empty-string-literal disallow empty string literals in character classes 🟢 🔵
no-extra-lookaround-assertions disallow unnecessary nested lookaround assertions 🟢 🔵 🔧
no-invisible-character disallow invisible raw character 🟢 🔵 🔧
no-legacy-features disallow legacy RegExp features 🟢 🔵
no-non-standard-flag disallow non-standard flags 🟢 🔵
no-obscure-range disallow obscure character ranges 🟢 🔵
no-octal disallow octal escape sequence 💡
no-standalone-backslash disallow standalone backslashes (\)
no-trivially-nested-assertion disallow trivially nested assertions 🟢 🔵 🔧
no-trivially-nested-quantifier disallow nested quantifiers that can be rewritten as one quantifier 🟢 🔵 🔧
no-unused-capturing-group disallow unused capturing group 🟢 🔵 🔧 💡
no-useless-character-class disallow character class with one character 🟢 🔵 🔧
no-useless-flag disallow unnecessary regex flags 🟢 🔵 🔧
no-useless-lazy disallow unnecessarily non-greedy quantifiers 🟢 🔵 🔧
no-useless-quantifier disallow quantifiers that can be removed 🟢 🔵 🔧 💡
no-useless-range disallow unnecessary character ranges 🟢 🔵 🔧
no-useless-set-operand disallow unnecessary elements in expression character classes 🟢 🔵 🔧
no-useless-string-literal disallow string disjunction of single characters in \q{...} 🟢 🔵 🔧
no-useless-two-nums-quantifier disallow unnecessary {n,m} quantifier 🟢 🔵 🔧
no-zero-quantifier disallow quantifiers with a maximum of zero 🟢 🔵 💡
optimal-lookaround-quantifier disallow the alternatives of lookarounds that end with a non-constant quantifier 🟢 🔵 💡
optimal-quantifier-concatenation require optimal quantifiers for concatenated quantifiers 🟢 🔵 🔧
prefer-escape-replacement-dollar-char enforces escape of replacement $ character ($$).
prefer-predefined-assertion prefer predefined assertion over equivalent lookarounds 🟢 🔵 🔧
prefer-quantifier enforce using quantifier 🔧
prefer-range enforce using character class range 🟢 🔵 🔧
prefer-regexp-exec enforce that RegExp#exec is used instead of String#match if no global flag is provided
prefer-regexp-test enforce that RegExp#test is used instead of String#match and RegExp#exec 🔧
prefer-set-operation prefer character class set operations instead of lookarounds 🟢 🔵 🔧
require-unicode-regexp enforce the use of the u flag 🔧
require-unicode-sets-regexp enforce the use of the v flag 🔧
simplify-set-operations require simplify set operations 🟢 🔵 🔧
sort-alternatives sort alternatives if order doesn't matter 🔧
use-ignore-case use the i flag if it simplifies the pattern 🟢 🔵 🔧

Stylistic Issues

Name Description 💼 ⚠️ 🔧 💡
grapheme-string-literal enforce single grapheme in string literal
hexadecimal-escape enforce consistent usage of hexadecimal escape 🔧
letter-case enforce into your favorite case 🔧
match-any enforce match any character style 🟢 🔵 🔧
no-useless-escape disallow unnecessary escape characters in RegExp 🟢 🔵 🔧
no-useless-non-capturing-group disallow unnecessary non-capturing group 🟢 🔵 🔧
prefer-character-class enforce using character class 🟢 🔵 🔧
prefer-d enforce using \d 🟢 🔵 🔧
prefer-lookaround prefer lookarounds over capturing group that do not replace 🔧
prefer-named-backreference enforce using named backreferences 🔧
prefer-named-capture-group enforce using named capture groups
prefer-named-replacement enforce using named replacement 🔧
prefer-plus-quantifier enforce using + quantifier 🟢 🔵 🔧
prefer-question-quantifier enforce using ? quantifier 🟢 🔵 🔧
prefer-result-array-groups enforce using result array groups 🔧
prefer-star-quantifier enforce using * quantifier 🟢 🔵 🔧
prefer-unicode-codepoint-escapes enforce use of unicode codepoint escapes 🟢 🔵 🔧
prefer-w enforce using \w 🟢 🔵 🔧
sort-character-class-elements enforces elements order in character class 🔧
sort-flags require regex flags to be sorted 🟢 🔵 🔧
unicode-escape enforce consistent usage of unicode escape or unicode codepoint escape 🔧
unicode-property enforce consistent naming of unicode properties 🔧

Removed

  • :no_entry: These rules have been removed in a previous major release, after they have been deprecated for a while.
Rule ID Replaced by Removed in version
no-assertion-capturing-group regexp/no-empty-capturing-group v2.0.0
no-useless-exactly-quantifier regexp/no-useless-quantifier, regexp/no-zero-quantifier v2.0.0
no-useless-non-greedy regexp/no-useless-lazy v2.0.0
order-in-character-class regexp/sort-character-class-elements v2.0.0
prefer-t regexp/control-character-escape v2.0.0

:gear: Settings

See Settings.

:traffic_light: Semantic Versioning Policy

eslint-plugin-regexp follows Semantic Versioning and ESLint's Semantic Versioning Policy.

:beers: Contributing

Welcome contributing!

Please use GitHub's Issues/PRs.

See CONTRIBUTING.md.

Development Tools

  • npm test runs tests and measures coverage.
  • npm run update runs in order to update readme and recommended configuration.
  • npm run new [new rule name] runs to create the files needed for the new rule.
  • npm run docs:watch starts the website locally.

:lock: License

See the LICENSE file for license rights and limitations (MIT).

changelog

eslint-plugin-regexp

2.7.0

Minor Changes

  • Add support for ES2025 duplicate named capturing groups (#752)

Patch Changes

  • Updates unicode property alias resource with latest (#766)

  • fix(regexp/no-useless-dollar-replacements): Correct capturing group not found grammar (#776)

2.6.0

Minor Changes

  • Improved compatibility of type information with typescript-eslint in config. (#740)

2.5.0

Minor Changes

  • Add regexp/unicode-property rule to enforce consistent naming of unicode properties (#722)

Patch Changes

  • Publish type declarations (#724)

2.4.0

Minor Changes

  • Change default configuration of prefer-d to ignore digits inside character classes. (#715)

2.3.0

Minor Changes

  • Add support for flat config (#702)

2.2.0

Minor Changes

  • Add allowNamed option to no-unused-capturing-group rule to allow for unused named capturing groups. (#689)

2.1.2

Patch Changes

  • Fixed false positives for hasIndices in regexp/no-unused-capturing-group (#676)

2.1.1

Patch Changes

  • Improved error message and docs for regexp/no-useless-non-capturing-group (#668)

2.1.0

Minor Changes

  • Improve detection of useless assertions for regexp/no-useless-assertion (#663)

  • Add suggestions for regexp/no-useless-assertion (#666)

Patch Changes

  • Fixed crash for v flag in regexp/optimal-quantifier-concatenation (#660)

  • Fixed minor mistake in message of no-misleading-unicode-character (#664)

2.0.0

Major Changes

  • Drop support for Node.js <=v17, and v19 (#550)

  • Add regexp/simplify-set-operations rule (#595)

  • Remove deprecated rules (#652)

  • Add regexp/no-useless-string-literal rule (#639)

  • Add regexp/no-empty-string-literal rule (#632)

  • Change recommended config (#552)

  • Drop support for ESLint < v8.44 (#558)

  • Add regexp/no-useless-set-operand rule (#625)

  • Add regexp/prefer-set-operation rule (#616)

  • Use jsdoc-type-pratt-parser instead of jsdoctypeparser (#651)

Minor Changes

  • Add support for v flag to regexp/no-dupe-disjunctions (#612)

  • Add support for v flag to regexp/optimal-quantifier-concatenation (#618)

  • Add support for v flag to regexp/no-non-standard-flag rule (#596)

  • Add regexp/grapheme-string-literal rule (#646)

  • Add support for v flag to regexp/prefer-character-class (#619)

  • Improve regexp/strict rule to ignore patterns with v flag (#591)

  • Add support for string literal to regexp/no-empty-alternative (#633)

  • Add support for v flag to regexp/prefer-d rule (#602)

  • Add support for v flag to regexp/use-ignore-case (#617)

  • Update refa, regexp-ast-analysis, and scslre (#568)

  • Add support for v flag to regexp/prefer-unicode-codepoint-escapes rule (#592)

  • Improve regexp/negation rule to report nested negation character classes (#595)

  • Add support for v flag to regexp/no-dupe-characters-character-class rule (#608)

  • Add support for v flag to regexp/no-useless-character-class rule (#593)

  • Add support for v flag to regexp/sort-character-class-elements rule (#588)

  • Add support for v flag to regexp/negation rule (#560)

  • Use Intl.Segmenter instead of grapheme-splitter (#642)

  • Add support for v flag to regexp/no-misleading-capturing-group (#620)

  • Add support for v flag to regexp/unicode-escape rule (#592)

  • Add support for v flag to regexp/match-any (#628)

  • Improve regexp/require-unicode-regexp rule to allow patterns with v flag (#586)

  • Add support for v flag to regexp/no-contradiction-with-assertion (#606)

  • Add support for v flag to regexp/no-empty-character-class (#637)

  • Add suggestions for regexp/no-lazy-ends (#624)

  • Improve regexp/sort-alternatives rule to add support for string alternatives and v flag (#587)

  • Add suggestions for regexp/optimal-lookaround-quantifier (#623)

  • Add support for v flag to regexp/no-useless-escape rule (#585)

  • Update @eslint-community/regexpp to v4.6 (#554)

  • Add support for v flag to regexp/no-misleading-unicode-character rule (#584)

  • Add regexp/require-unicode-sets-regexp rule (#598)

  • Add suggestions for regexp/no-empty-alternative (#621)

  • prefer-w: Add support for v flag (#578)

  • Added suggestions for regexp/no-escape-backspace (#622)

  • Improve regexp/no-invalid-regexp rule to check for unknown pattern flags. (#583)

  • Add support for v flag to regexp/prefer-predefined-assertion (#611)

Patch Changes

  • fix: update regexpp (#634)

  • Fixed incorrect default of ignoreSticky option in regexp/no-super-linear-move (#630)

  • Use new refa AST transformers and fixed max character for v-flag regexes in no-dupe-disjunctions and no-super-linear-move. (#569)

  • fix: autofix for incorrect no-escaping in regexp/no-useless-string-literal (#645)

  • Fix typo in no-useless-non-capturing-group (#555)

  • Use new ESLint API (#654)

  • Account for v flag in 2 util methods (#570)

  • Add more v flag tests (#627)

  • Fix parseFlags (#571)

2.0.0-next.15

Patch Changes

  • Use new ESLint API (#654)

2.0.0-next.14

Major Changes

  • Remove deprecated rules (#652)

  • Use jsdoc-type-pratt-parser instead of jsdoctypeparser (#651)

2.0.0-next.13

Minor Changes

  • Add regexp/grapheme-string-literal rule (#646)

  • Use Intl.Segmenter instead of grapheme-splitter (#642)

Patch Changes

  • fix: autofix for incorrect no-escaping in regexp/no-useless-string-literal (#645)

2.0.0-next.12

Major Changes

  • Add regexp/no-useless-string-literal rule (#639)

Minor Changes

  • Add support for v flag to regexp/no-empty-character-class (#637)

2.0.0-next.11

Major Changes

  • Add regexp/simplify-set-operations rule (#595)

  • Add regexp/no-empty-string-literal rule (#632)

Minor Changes

  • Add support for string literal to regexp/no-empty-alternative (#633)

  • Improve regexp/negation rule to report nested negation character classes (#595)

Patch Changes

  • fix: update regexpp (#634)

2.0.0-next.10

Major Changes

  • Add regexp/no-useless-set-operand rule (#625)

Minor Changes

  • Add support for v flag to regexp/match-any (#628)

Patch Changes

  • Fixed incorrect default of ignoreSticky option in regexp/no-super-linear-move (#630)

  • Add more v flag tests (#627)

2.0.0-next.9

Major Changes

  • Add regexp/prefer-set-operation rule (#616)

Minor Changes

  • Add support for v flag to regexp/optimal-quantifier-concatenation (#618)

  • Add support for v flag to regexp/prefer-character-class (#619)

  • Add support for v flag to regexp/use-ignore-case (#617)

  • Add support for v flag to regexp/no-misleading-capturing-group (#620)

  • Add suggestions for regexp/no-lazy-ends (#624)

  • Add suggestions for regexp/optimal-lookaround-quantifier (#623)

  • Add suggestions for regexp/no-empty-alternative (#621)

  • Added suggestions for regexp/no-escape-backspace (#622)

2.0.0-next.8

Minor Changes

  • Add support for v flag to regexp/sort-character-class-elements rule (#588)

2.0.0-next.7

Minor Changes

  • Add support for v flag to regexp/no-dupe-disjunctions (#612)

  • Add support for v flag to regexp/no-dupe-characters-character-class rule (#608)

  • Add support for v flag to regexp/no-useless-character-class rule (#593)

  • Improve regexp/sort-alternatives rule to add support for string alternatives and v flag (#587)

  • Add regexp/require-unicode-sets-regexp rule (#598)

  • Add support for v flag to regexp/prefer-predefined-assertion (#611)

2.0.0-next.6

Minor Changes

  • Add support for v flag to regexp/prefer-unicode-codepoint-escapes rule (#592)

  • Add support for v flag to regexp/unicode-escape rule (#592)

Patch Changes

  • Add support for v flag to regexp/no-contradiction-with-assertion (#606)

2.0.0-next.5

Minor Changes

  • Add support for v flag to regexp/prefer-d rule (#602)

  • Add support for v flag to regexp/negation rule (#560)

  • Improve regexp/require-unicode-regexp rule to allow patterns with v flag (#586)

  • Add support for v flag to regexp/no-useless-escape rule (#585)

  • Add support for v flag to regexp/no-misleading-unicode-character rule (#584)

  • prefer-w: Add support for v flag (#578)

2.0.0-next.4

Minor Changes

  • Add support for v flag to regexp/no-non-standard-flag rule (#596)

  • Improve regexp/strict rule to ignore patterns with v flag (#591)

2.0.0-next.3

Minor Changes

  • Improve regexp/no-invalid-regexp rule to check for unknown pattern flags. (#583)

2.0.0-next.2

Patch Changes

  • Use new refa AST transformers and fixed max character for v-flag regexes in no-dupe-disjunctions and no-super-linear-move. (#569)

  • Account for v flag in 2 util methods (#570)

  • Fix parseFlags (#571)

2.0.0-next.1

Major Changes

  • Drop support for ESLint < v8.44 (#558)

Minor Changes

  • Update refa, regexp-ast-analysis, and scslre (#568)

Patch Changes

  • Fix typo in no-useless-non-capturing-group (#555)

2.0.0-next.0

Major Changes

  • Drop support for Node.js <=v17, and v19 (#550)

  • Change recommended config (#552)

Minor Changes

  • Update @eslint-community/regexpp to v4.6 (#554)