Introduction
eslint-plugin-regexp is ESLint plugin for finding RegExp mistakes and RegExp style guide violations.
๐ 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.
๐ Documentation
See documents.
๐ฟ Installation
npm install --save-dev eslint eslint-plugin-regexp
Requirements
- ESLint v8.44.0 and above
- Node.js v18.x, v20.x and above
๐ 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.
โ Rules
๐ผ Configurations enabled in.
โ
Set in the plugin:regexp/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 | ๐ง |
Removed
- โ 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 |
โ๏ธ Settings
See Settings.
๐ฅ Semantic Versioning Policy
eslint-plugin-regexp follows Semantic Versioning and ESLint's Semantic Versioning Policy.
๐ป 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.
๐ License
See the LICENSE file for license rights and limitations (MIT).