What is eslint-plugin-regexp?
eslint-plugin-regexp is an ESLint plugin that provides linting rules for regular expressions. It helps developers write more efficient, readable, and secure regular expressions by catching common mistakes and suggesting improvements.
What are eslint-plugin-regexp's main functionalities?
No Unnecessary Escape
This rule disallows unnecessary escape characters in regular expressions. In the example, the backslash before the dot is unnecessary and should be removed.
/* eslint regexp/no-useless-escape: 'error' */
const regex = /\./;
No Obscure Character Class
This rule disallows obscure character class ranges that can be confusing or error-prone. In the example, the character class is clear and does not contain obscure ranges.
/* eslint regexp/no-obscure-range: 'error' */
const regex = /[a-zA-Z]/;
No Unnecessary Quantifier
This rule disallows unnecessary quantifiers in regular expressions. In the example, the quantifier {1} is unnecessary and should be removed.
/* eslint regexp/no-useless-quantifier: 'error' */
const regex = /a{1}/;
No Empty Character Class
This rule disallows empty character classes in regular expressions. In the example, the character class is empty and should be corrected.
/* eslint regexp/no-empty-character-class: 'error' */
const regex = /[]/;
Other packages similar to eslint-plugin-regexp
eslint-plugin-security
eslint-plugin-security is an ESLint plugin that helps identify potential security issues in JavaScript code. While it covers a broader range of security concerns, it includes some rules related to regular expressions, such as detecting potential ReDoS (Regular Expression Denial of Service) vulnerabilities. It is more general in scope compared to eslint-plugin-regexp.
eslint-plugin-unicorn
eslint-plugin-unicorn is a collection of various ESLint rules aimed at improving code quality and consistency. It includes some rules for regular expressions, such as preventing potential ReDoS attacks and suggesting more readable patterns. However, it is not as focused on regular expressions as eslint-plugin-regexp.
Introduction
eslint-plugin-regexp is ESLint plugin for finding RegExp mistakes and RegExp style guide violations.
: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.
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.
module.exports = {
"plugins": [
"regexp"
],
"extends": [
"plugin:regexp/recommended"
]
}
Advanced Configuration
Override/add specific rules configurations. See also: http://eslint.org/docs/user-guide/configuring.
import * as regexpPlugin from "eslint-plugin-regexp"
export default [
{
plugins: { regexp: regexpPlugin }
rules: {
"regexp/rule-name": "error"
}
}
];
module.exports = {
"plugins": [
"regexp"
],
"rules": {
"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
Best Practices
Stylistic Issues
Removed
- :no_entry: These rules have been removed in a previous major release, after they have been deprecated for a while.
: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).