prefer-regex-literals
禁止使用 RegExp 构造函数以支持正则表达式字面
此规则报告的一些问题可通过编辑器 建议 手动修复
创建正则表达式有两种方法:
🌐 There are two ways to create a regular expression:
- 正则表达式字面量,例如,
/abc/u。 RegExp构造函数,例如new RegExp("abc", "u")或RegExp("abc", "u")。
当你想动态生成模式时,构造函数特别有用,因为它接受字符串参数。
🌐 The constructor function is particularly useful when you want to dynamically generate the pattern, because it takes string arguments.
在使用带有字符串字面量的构造函数时,不要忘记字符串转义规则仍然适用。如果你想在模式中放置一个反斜杠,你需要在字符串字面量中对它进行转义。因此,以下是等效的:
🌐 When using the constructor function with string literals, don’t forget that the string escaping rules still apply. If you want to put a backslash in the pattern, you need to escape it in the string literal. Thus, the following are equivalent:
new RegExp("^\\d\\.$");
/^\d\.$/;
// matches "0.", "1.", "2." ... "9."
在上面的例子中,正则表达式字面量更容易阅读和理解。此外,省略字符串字面量中的额外 \ 是一个常见错误,这会产生一个完全不同的正则表达式:
🌐 In the above example, the regular expression literal is easier to read and reason about.
Also, it’s a common mistake to omit the extra \ in the string literal, which would produce a completely different regular expression:
new RegExp("^\d\.$");
// equivalent to /^d.$/, matches "d1", "d2", "da", "db" ...
当一个正则表达式事先已知时,最佳实践是避免在正则表达式符号上方使用字符串字面量表示法,而使用正则表达式字面量而不是构造函数。
🌐 When a regular expression is known in advance, it is considered a best practice to avoid the string literal notation on top of the regular expression notation, and use regular expression literals instead of the constructor function.
规则详情
🌐 Rule Details
此规则禁止在使用 RegExp 构造函数时将字符串字面量作为其参数。
🌐 This rule disallows the use of the RegExp constructor function with string literals as its arguments.
此规则也禁止在没有表达式的情况下,将 RegExp 构造函数与模板字面量一起使用,以及将 String.raw 标签模板字面量与模板字面量一起使用而不带表达式。
🌐 This rule also disallows the use of the RegExp constructor function with template literals without expressions
and String.raw tagged template literals without expressions.
该规则并不禁止所有对 RegExp 构造函数的使用。它仍然应当用于动态生成的正则表达式。
🌐 The rule does not disallow all use of the RegExp constructor. It should be still used for
dynamically generated regular expressions.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint prefer-regex-literals: "error"*/
new RegExp("abc");
new RegExp("abc", "u");
RegExp("abc");
RegExp("abc", "u");
new RegExp("\\d\\d\\.\\d\\d\\.\\d\\d\\d\\d");
RegExp(`^\\d\\.$`);
new RegExp(String.raw`^\d\.$`);
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint prefer-regex-literals: "error"*/
/abc/;
/abc/u;
/\d\d\.\d\d\.\d\d\d\d/;
/^\d\.$/;
// RegExp constructor is allowed for dynamically generated regular expressions
new RegExp(pattern);
RegExp("abc", flags);
new RegExp(prefix + "abc");
RegExp(`${prefix}abc`);
new RegExp(String.raw`^\d\. ${suffix}`);
选项
🌐 Options
此规则有一个对象选项:
🌐 This rule has an object option:
disallowRedundantWrapping设置为true时,还会检查不必要封装的正则表达式字面量(默认值false)。
disallowRedundantWrapping
默认情况下,此规则不会检查正则表达式字面量是否被不必要地封装在 RegExp 构造函数调用中。当选项 disallowRedundantWrapping 设置为 true 时,该规则也会禁止这种不必要的模式。
🌐 By default, this rule doesn’t check when a regex literal is unnecessarily wrapped in a RegExp constructor call. When the option disallowRedundantWrapping is set to true, the rule will also disallow such unnecessary patterns.
{ "disallowRedundantWrapping": true } 的 incorrect 代码示例
🌐 Examples of incorrect code for { "disallowRedundantWrapping": true }
/*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/
new RegExp(/abc/);
new RegExp(/abc/, 'u');
{ "disallowRedundantWrapping": true } 的 correct 代码示例
🌐 Examples of correct code for { "disallowRedundantWrapping": true }
/*eslint prefer-regex-literals: ["error", {"disallowRedundantWrapping": true}]*/
/abc/;
/abc/u;
new RegExp(/abc/, flags);
版本
此规则是在 ESLint v6.4.0 中引入。