JS.BASE.WRAP.REGEX

Require parenthesis around regex literals

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

Copy
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:

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

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

Example of correct code for this rule:

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

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

The content on this page is adapted from the ESLint User Guide. Copyright © OpenJS Foundation and other contributors, www.openjsf.org. All rights reserved. https://eslint.org/docs/rules/