eslint-plugin-regexp

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

Usage no npm install needed!

<script type="module">
  import eslintPluginRegexp from 'https://cdn.skypack.dev/eslint-plugin-regexp';
</script>

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.

You can check on the Online DEMO.

:book: Documentation

See documents.

:cd: Installation

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

Requirements

  • ESLint v6.0.0 and above
  • Node.js v12.x, v14.x and above

:book: Usage

Add regexp to the plugins section of your .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

The plugin:regexp/recommended config enables a subset of the rules that should be most useful to most users. See lib/configs/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.

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

Using "plugin:regexp/all"

The 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/all.ts for more details.

:white_check_mark: Rules

The --fix option on the command line automatically fixes problems reported by rules which have a wrench :wrench: below. The rules with the following star :star: are included in the plugin:regexp/recommended config.

Possible Errors

Rule ID Description
regexp/no-contradiction-with-assertion disallow elements that contradict assertions
regexp/no-control-character disallow control characters
regexp/no-dupe-disjunctions disallow duplicate disjunctions :star:
regexp/no-empty-alternative disallow alternatives without elements :star:
regexp/no-empty-capturing-group disallow capturing group that captures empty. :star:
regexp/no-empty-character-class disallow character classes that match no characters
regexp/no-empty-group disallow empty group :star:
regexp/no-empty-lookarounds-assertion disallow empty lookahead assertion or empty lookbehind assertion :star:
regexp/no-escape-backspace disallow escape backspace ([\b]) :star:
regexp/no-invalid-regexp disallow invalid regular expression strings in RegExp constructors :star:
regexp/no-lazy-ends disallow lazy quantifiers at the end of an expression :star:
regexp/no-misleading-unicode-character disallow multi-code-point characters in character classes and quantifiers :wrench:
regexp/no-optional-assertion disallow optional assertions :star:
regexp/no-potentially-useless-backreference disallow backreferences that reference a group that might not be matched :star:
regexp/no-super-linear-backtracking disallow exponential and polynomial backtracking :star::wrench:
regexp/no-super-linear-move disallow quantifiers that cause quadratic moves
regexp/no-useless-assertions disallow assertions that are known to always accept (or reject) :star:
regexp/no-useless-backreference disallow useless backreferences in regular expressions :star:
regexp/no-useless-dollar-replacements disallow useless $ replacements in replacement string :star:
regexp/strict disallow not strictly valid regular expressions :star::wrench:

Best Practices

Rule ID Description
regexp/confusing-quantifier disallow confusing quantifiers :star:
regexp/control-character-escape enforce consistent escaping of control characters :star::wrench:
regexp/negation enforce use of escapes on negation :star::wrench:
regexp/no-dupe-characters-character-class disallow duplicate characters in the RegExp character class :star::wrench:
regexp/no-invisible-character disallow invisible raw character :star::wrench:
regexp/no-legacy-features disallow legacy RegExp features :star:
regexp/no-non-standard-flag disallow non-standard flags :star:
regexp/no-obscure-range disallow obscure character ranges :star:
regexp/no-octal disallow octal escape sequence
regexp/no-standalone-backslash disallow standalone backslashes (\)
regexp/no-trivially-nested-assertion disallow trivially nested assertions :star::wrench:
regexp/no-trivially-nested-quantifier disallow nested quantifiers that can be rewritten as one quantifier :star::wrench:
regexp/no-unused-capturing-group disallow unused capturing group :star::wrench:
regexp/no-useless-character-class disallow character class with one character :star::wrench:
regexp/no-useless-flag disallow unnecessary regex flags :star::wrench:
regexp/no-useless-lazy disallow unnecessarily non-greedy quantifiers :star::wrench:
regexp/no-useless-quantifier disallow quantifiers that can be removed :star::wrench:
regexp/no-useless-range disallow unnecessary range of characters by using a hyphen :star::wrench:
regexp/no-useless-two-nums-quantifier disallow unnecessary {n,m} quantifier :star::wrench:
regexp/no-zero-quantifier disallow quantifiers with a maximum of zero :star:
regexp/optimal-lookaround-quantifier disallow the alternatives of lookarounds that end with a non-constant quantifier :star:
regexp/optimal-quantifier-concatenation require optimal quantifiers for concatenated quantifiers :star::wrench:
regexp/prefer-escape-replacement-dollar-char enforces escape of replacement $ character ($).
regexp/prefer-predefined-assertion prefer predefined assertion over equivalent lookarounds :star::wrench:
regexp/prefer-quantifier enforce using quantifier :wrench:
regexp/prefer-range enforce using character class range :star::wrench:
regexp/prefer-regexp-exec enforce that RegExp#exec is used instead of String#match if no global flag is provided
regexp/prefer-regexp-test enforce that RegExp#test is used instead of String#match and RegExp#exec :wrench:
regexp/require-unicode-regexp enforce the use of the u flag :wrench:
regexp/sort-alternatives sort alternatives if order doesn't matter :wrench:
regexp/use-ignore-case use the i flag if it simplifies the pattern :wrench:

Stylistic Issues

Rule ID Description
regexp/hexadecimal-escape enforce consistent usage of hexadecimal escape :wrench:
regexp/letter-case enforce into your favorite case :wrench:
regexp/match-any enforce match any character style :star::wrench:
regexp/no-useless-escape disallow unnecessary escape characters in RegExp :star::wrench:
regexp/no-useless-non-capturing-group disallow unnecessary Non-capturing group :star::wrench:
regexp/prefer-character-class enforce using character class :star::wrench:
regexp/prefer-d enforce using \d :star::wrench:
regexp/prefer-lookaround prefer lookarounds over capturing group that do not replace :wrench:
regexp/prefer-named-backreference enforce using named backreferences :wrench:
regexp/prefer-named-capture-group enforce using named capture groups
regexp/prefer-named-replacement enforce using named replacement :wrench:
regexp/prefer-plus-quantifier enforce using + quantifier :star::wrench:
regexp/prefer-question-quantifier enforce using ? quantifier :star::wrench:
regexp/prefer-result-array-groups enforce using result array groups :wrench:
regexp/prefer-star-quantifier enforce using * quantifier :star::wrench:
regexp/prefer-unicode-codepoint-escapes enforce use of unicode codepoint escapes :star::wrench:
regexp/prefer-w enforce using \w :star::wrench:
regexp/sort-character-class-elements enforces elements order in character class :wrench:
regexp/sort-flags require regex flags to be sorted :star::wrench:
regexp/unicode-escape enforce consistent usage of unicode escape or unicode codepoint escape :wrench:

Deprecated

  • :warning: We're going to remove deprecated rules in the next major release. Please migrate to successor/new rules.
  • :innocent: We don't fix bugs which are in deprecated rules since we don't have enough resources.
Rule ID Replaced by
regexp/no-assertion-capturing-group regexp/no-empty-capturing-group
regexp/no-useless-exactly-quantifier regexp/no-useless-quantifier, regexp/no-zero-quantifier
regexp/no-useless-non-greedy regexp/no-useless-lazy
regexp/order-in-character-class regexp/sort-character-class-elements
regexp/prefer-t regexp/control-character-escape

: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).