
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
eslint-plugin-clean-regex
Advanced tools
An ESLint plugin for writing better regular expressions.
An ESLint plugin for writing better regular expressions.
This project is deprecated.
Please use eslint-plugin-regexp
instead.
eslint-plugin-clean-regex
and eslint-plugin-regexp
have joined forces. We decided to work together on one ESLint plugin for JavaScript regexes. Since maintaining two plugins with similar rules takes too much work, I decided to stop working on eslint-plugin-clean-regex
.
As of right now, eslint-plugin-regexp
supports all rules of eslint-plugin-clean-regex
along improvements to those rules and with many more useful rules.
See the migration guide.
This is an ESLint plugin to lint JavaScript regular expressions. Its goal is to help both beginners and experts to write better regular expressions by pointing out errors and suggesting improvements.
The plugin offers rules for possible errors, best practices, and coding style in regular expressions.
Right now, this project is still young (and many rules are opinionated). Feel free to open an issue if you think rules are too strict/lax/inflexible. Suggestions and feature requests are welcome as well!
You'll need to install ESLint and eslint-plugin-clean-regex
:
$ npm i eslint eslint-plugin-clean-regex --save-dev
Note: If you installed ESLint globally (using the -g
flag) then you must also install eslint-plugin-clean-regex
globally.
Add clean-regex
to the plugins section of your .eslintrc
configuration file (you can omit the eslint-plugin-
prefix) and configure the rules you want:
{
"plugins": [
"clean-regex"
],
"rules": {
"clean-regex/rule-name": 2
}
}
You can also use the recommended config:
{
"plugins": [
"clean-regex"
],
"extends": [
"plugin:clean-regex/recommended"
]
}
The setting of every rule in the recommended config can be found in the table below.
Some highlights of the working and working-together of rules in the recommended config.
Before:
- /[0-9]/i
- /[^\s]/
- /[a-fA-F0-9]/i
- /[a-zA-Z0-9_-]/
- /[a-z\d\w]/
- /[\S\d]/
- /[\w\p{ASCII}]/u
After:
- /\d/
- /\S/
- /[a-f0-9]/i
- /[\w-]/
- /\w/
- /\S/
- /\p{ASCII}/u
Before:
- /(?:\w|\d)+/
- /(?:a|(b)|c|(?:d)|(?:ee)){0,}/
- /(?<!\w)a+(?=$)/mi
- /[\s\S]#[\0-\uFFFF]/ysi
- /\d*\w(?:[a-z_]|\d+)*/im
After:
- /\w+/
- /(?:[acd]|(b)|ee)*/
- /\ba+$/im
- /.#./sy
- /\w+/
- /\1(a)/ // `\1` won't work
- /a+b*?/ // `b*?` can be removed
- /(?:\b)?a/ // `(?:\b)?` can be removed
- /[a-z]+|Foo/i // `Foo` can be removed
- /(?=a?)\w\Ba/ // `(?=a?)` and `\B` always accept and can be removed
- /[*/+-^&|]/ // `+-^` will match everything from \x2B to \x5E including all character A to Z
Fixable rules are denoted with a :wrench:.
Rule | Description | |
---|---|---|
confusing-quantifier | Warn about confusing quantifiers. | |
disjoint-alternatives | Disallow different alternatives that can match the same words. | |
no-empty-alternative | Disallow alternatives without elements. | |
no-empty-backreference | Disallow backreferences that will always be replaced with the empty string. | |
no-empty-lookaround | Disallow lookarounds that can match the empty string. | |
no-lazy-ends | Disallow lazy quantifiers at the end of an expression. | |
no-obscure-range | Disallow obscure ranges in character classes. | |
no-octal-escape | Disallow octal escapes outside of character classes. | |
no-optional-assertion | Disallow optional assertions. | |
no-potentially-empty-backreference | Disallow backreferences that reference a group that might not be matched. | |
no-unnecessary-assertions | Disallow assertions that are known to always accept (or reject). | |
:wrench: | no-zero-quantifier | Disallow quantifiers with a maximum of 0. |
optimal-lookaround-quantifier | Disallows the alternatives of lookarounds that end with a non-constant quantifier. |
Rule | Description | |
---|---|---|
:wrench: | consistent-match-all-characters | Use one character class consistently whenever all characters have to be matched. |
:wrench: | identity-escape | How to handle identity escapes. |
no-constant-capturing-group | Disallow capturing groups that can match only one word. | |
:wrench: | no-trivially-nested-lookaround | Disallow lookarounds that only contain another assertion. |
:wrench: | no-trivially-nested-quantifier | Disallow nested quantifiers that can be rewritten as one quantifier. |
:wrench: | no-unnecessary-character-class | Disallow unnecessary character classes. |
:wrench: | no-unnecessary-flag | Disallow unnecessary regex flags. |
:wrench: | no-unnecessary-group | Disallow unnecessary non-capturing groups. |
:wrench: | no-unnecessary-lazy | Disallow unnecessarily lazy quantifiers. |
:wrench: | no-unnecessary-quantifier | Disallow unnecessary quantifiers. |
:wrench: | optimal-concatenation-quantifier | Use optimal quantifiers for concatenated quantified characters. |
:wrench: | optimized-character-class | Disallows unnecessary elements in character classes. |
:wrench: | prefer-character-class | Prefer character classes wherever possible instead of alternations. |
:wrench: | prefer-predefined-assertion | Prefer predefined assertions over equivalent lookarounds. |
:wrench: | prefer-predefined-character-set | Prefer predefined character sets instead of their more verbose form. |
:wrench: | prefer-predefined-quantifiers | Prefer predefined quantifiers (+*?) instead of their more verbose form. |
:wrench: | simple-constant-quantifier | Prefer simple constant quantifiers over the range form. |
:wrench: | sort-flags | Requires the regex flags to be sorted. |
0.5.2 (2021-10-11)
The project is now officially deprecated and will not be worked on anymore.
Please use eslint-plugin-regexp instead. You can find a migration guide here.
FAQs
An ESLint plugin for writing better regular expressions.
The npm package eslint-plugin-clean-regex receives a total of 389 weekly downloads. As such, eslint-plugin-clean-regex popularity was classified as not popular.
We found that eslint-plugin-clean-regex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.