Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Latest commit

 

History

History
50 lines (35 loc) · 1.35 KB

no-unnecessary-character-class.md

File metadata and controls

50 lines (35 loc) · 1.35 KB

no-unnecessary-character-class 🔧

Disallow unnecessary character classes.

configuration in plugin:clean-regex/recommended: "warn"

Source file
Test file

Description

Unnecessary character classes contain only one character and can be trivially removed. E.g. [a], [\x61], [\?].

avoidEscape

Sometimes characters have to be escaped, in order to remove the character class (e.g. a[+] -> a\+). The automatic escaping can be disabled by using { avoidEscape: true } in the rule configuration.

Note: This option does not affect characters already escaped in the character class (e.g. a[\+] -> a\+).

Note: \b means backspace (\x08) inside of character classes but it will interpreted as a boundary assertion anywhere else, so it will be escaped as \x08.

With avoidEscape: false

/a[+]/ -> /a\+/
/a[.]/ -> /a\./
/[\b]/ -> /\x08/

/a[\s]/ -> /a\s/
/a[\+]/ -> /a\+/

With avoidEscape: true

/a[+]/ -> /a[+]/
/a[.]/ -> /a[.]/
/[\b]/ -> /[\b]/

/a[\s]/ -> /a\s/
/a[\+]/ -> /a\+/