pageClass | sidebarDepth | title | description | since |
---|---|---|---|---|
rule-details |
0 |
regexp/no-obscure-range |
disallow obscure character ranges |
v0.9.0 |
💼 This rule is enabled in the ✅ plugin:regexp/recommended
config.
disallow obscure character ranges
The character range operator (the -
inside character classes) can easily be misused (mostly unintentionally) to construct non-obvious character class. This rule will disallow all non-obvious uses of the character range operator.
/* eslint regexp/no-obscure-range: "error" */
/* ✓ GOOD */
var foo = /[a-z]/;
var foo = /[J-O]/;
var foo = /[1-9]/;
var foo = /[\x00-\x40]/;
var foo = /[\0-\uFFFF]/;
var foo = /[\0-\u{10FFFF}]/u;
var foo = /[\1-\5]/;
var foo = /[\cA-\cZ]/;
/* ✗ BAD */
var foo = /[A-\x43]/;
var foo = /[\41-\x45]/;
var foo = /[!-$]/;
var foo = /[😀-😄]/u;
{
"regexp/no-obscure-range": ["error",
{
"allowed": "alphanumeric" // or "all" or [...]
}
]
}
This option can be used to override the allowedCharacterRanges setting.
It allows all values that the allowedCharacterRanges setting allows.
/* eslint regexp/no-obscure-range: ["error", { "allowed": "alphanumeric" }] */
/* ✓ GOOD */
var foo = /[a-z]/;
var foo = /[J-O]/;
var foo = /[1-9]/;
/* ✗ BAD */
var foo = /[A-\x43]/;
var foo = /[\41-\x45]/;
var foo = /[!-$]/;
var foo = /[😀-😄]/u;
/* eslint regexp/no-obscure-range: ["error", { "allowed": "all" }] */
/* ✓ GOOD */
var foo = /[a-z]/;
var foo = /[J-O]/;
var foo = /[1-9]/;
var foo = /[!-$]/;
var foo = /[😀-😄]/u;
/* ✗ BAD */
var foo = /[A-\x43]/;
var foo = /[\41-\x45]/;
/* eslint regexp/no-obscure-range: ["error", { "allowed": [ "alphanumeric", "😀-😏" ] }] */
/* ✓ GOOD */
var foo = /[a-z]/;
var foo = /[J-O]/;
var foo = /[1-9]/;
var foo = /[😀-😄]/u;
/* ✗ BAD */
var foo = /[A-\x43]/;
var foo = /[\41-\x45]/;
var foo = /[!-$]/;
This rule was introduced in eslint-plugin-regexp v0.9.0