no-empty-character-class
不允許在正規表示式中使用空的字元類別
✅ 推薦
在設定檔中使用來自 @eslint/js
的 recommended
設定會啟用此規則
因為正規表示式中的空字元類別不會匹配任何內容,它們可能是輸入錯誤。
var foo = /^abc[]/;
規則詳情
此規則不允許在正規表示式中使用空的字元類別。
此規則的錯誤程式碼範例
在 Playground 中開啟
/*eslint no-empty-character-class: "error"*/
.test("abcdefg"); // false
"abcdefg".match(); // null
.test("abcdefg"); // false
"abcdefg".match(); // null
.test("abcdefg"); // false
"abcdefg".match(); // null
.test("abcdefg"); // false
"abcdefg".match(); // null
const regex = ;
regex.test("abcdefg"); // true, the nested `[]` has no effect
"abcdefg".match(regex); // ["abcd"]
regex.test("abcefg"); // false, the nested `[]` has no effect
"abcefg".match(regex); // null
regex.test("abc"); // false, the nested `[]` has no effect
"abc".match(regex); // null
此規則的正確程式碼範例
在 Playground 中開啟
/*eslint no-empty-character-class: "error"*/
/^abc/.test("abcdefg"); // true
"abcdefg".match(/^abc/); // ["abc"]
/^abc[a-z]/.test("abcdefg"); // true
"abcdefg".match(/^abc[a-z]/); // ["abcd"]
/^abc[^]/.test("abcdefg"); // true
"abcdefg".match(/^abc[^]/); // ["abcd"]
已知限制
此規則不會回報呼叫 RegExp
建構子的字串參數中的空字元類別。
當此規則回報正確程式碼時的假陰性範例
/*eslint no-empty-character-class: "error"*/
var abcNeverMatches = new RegExp("^abc[]");
版本
此規則在 ESLint v0.22.0 中引入。