Require Regex Literals to be Wrapped (wrap-regex)

要求正则表达式被包裹起来 (wrap-regex)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

When a regular expression is used in certain situations, it can end up looking like a division operator. For example:

在某些情况下,使用正则表达式时,它看起来会像一个除法运算符。例如:

function a() {
    return /foo/.test("bar");
}

Rule Details

This is used to disambiguate the slash operator and facilitates more readable code.

该规则旨在消除斜线运算符造成的歧义,增加代码的可读性。

Example of incorrect code for this rule:

错误 代码示例:

/*eslint wrap-regex: "error"*/

function a() {
    return /foo/.test("bar");
}

Example of correct code for this rule:

正确 代码示例:

/*eslint wrap-regex: "error"*/

function a() {
    return (/foo/).test("bar");
}

Version

This rule was introduced in ESLint 0.1.0.

该规则在 ESLint 0.1.0 中被引入。

Resources