prefer-regex-literals

禁止使用 RegExp 构造函数以支持正则表达式字面

💡 hasSuggestions

此规则报告的一些问题可通过编辑器建议手动修复

创建正则表达式有两种方法:

¥There are two ways to create a regular expression:

  • 正则表达式字面,例如 /abc/u

    ¥Regular expression literals, e.g., /abc/u.

  • RegExp 构造函数,例如 new RegExp("abc", "u")RegExp("abc", "u")

    ¥The RegExp constructor function, e.g., new RegExp("abc", "u") or 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 set to true additionally checks for unnecessarily wrapped regex literals (Default false).

disallowRedundantWrapping

默认情况下,当正则表达式字面不必要地封装在 RegExp 构造函数调用中时,此规则不会检查。When the option disallowRedundantWrapping is set to true, the rule will also disallow such unnecessary patterns.

¥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 中引入。

进阶读物

资源

ESLint 中文网
粤ICP备13048890号