Skip to content

Latest commit

 

History

History
232 lines (157 loc) · 4.82 KB

no-useless-flag.md

File metadata and controls

232 lines (157 loc) · 4.82 KB
pageClass sidebarDepth title description since
rule-details
0
regexp/no-useless-flag
disallow unnecessary regex flags
v0.9.0

regexp/no-useless-flag

⚠️ This rule warns in the ✅ plugin:regexp/recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

disallow unnecessary regex flags

📖 Rule Details

This will point out present regex flags that do not change the pattern.

i flag (ignoreCase)

The i flag is only necessary if the pattern contains any characters with case variations. If the pattern contains no such characters, the flag will be unnecessary. E.g. /\.{3}/i

/* eslint regexp/no-useless-flag: "error" */

/* ✓ GOOD */
var foo = /a|b/i;

/* ✗ BAD */
var foo = /\.{3}/i;
var foo = /\w+/i;

m flag (multiline)

The m flag changes the behavior of the ^ and $ assertions. If the pattern doesn't contain these anchors, the m flag will be unnecessary. E.g. /foo|[^\r\n]*/m

/* eslint regexp/no-useless-flag: "error" */

/* ✓ GOOD */
var foo = /^foo$/m;

/* ✗ BAD */
var foo = /foo|[^\r\n]*/m;
var foo = /a|b/m;

s flag (dotAll)

The s flag makes the dot (.) match all characters instead of the usually non-line-terminator characters. If the pattern doesn't contain a dot character set, the s flag will be unnecessary. E.g. /[.:]/s

/* eslint regexp/no-useless-flag: "error" */

/* ✓ GOOD */
var foo = /a.*?b/s;

/* ✗ BAD */
var foo = /[.:]/s;
var foo = /^foo$/s;

g flag (global)

The g flag is used when you need to test a regular expression against all possible string match. If not, it will be unnecessary.

/* eslint regexp/no-useless-flag: "error" */

/* ✓ GOOD */
const regex1 = /foo/g;
const str = 'table football, foosball';
while ((array = regex1.exec(str)) !== null) {
  //
}

const regex2 = /foo/g;
regex2.test(string);
regex2.test(string);

str.replace(/foo/g, 'bar');
str.replaceAll(/foo/g, 'bar');

/* ✗ BAD */
/foo/g.test(string);
const regex3 = /foo/g;
regex3.test(string); // You have used it only once.

/foo/g.exec(string);
const regex4 = /foo/g;
regex4.exec(string); // You have used it only once.

new RegExp('foo', 'g').test(string);

str.search(/foo/g);

y flag (sticky)

The y flag is used when you need to do a sticky search. If not, it will be unnecessary.

/* eslint regexp/no-useless-flag: "error" */

/* ✓ GOOD */
const regex1 = /foo/y;
const str = 'table football, foosball';
regex1.lastIndex = 6
var array = regex1.exec(str)

const regex2 = /foo/y;
regex2.test(string);
regex2.test(string);

str.replace(/foo/y, 'bar');
str.replaceAll(/foo/gy, 'bar');

const regexp3 = /foo/y
str.search(regexp3)

/* ✗ BAD */
str.split(/foo/y);

other flags

No other flags will be checked.

🔧 Options

{
  "regexp/no-useless-flag": ["error",
    {
      "ignore": [], // An array of "i", "m", "s", "g" and "y".
      "strictTypes": true
    }
  ]
}
  • ignore ... An array of flags to ignore from the check.
  • strictTypes ... If true, strictly check the type of object to determine if the regex instance was used in search() and split(). Default is true. This option is only effective for verifying the g and y flags.
    This option is always on when using TypeScript.

"ignore": ["s", "g"]

/* eslint regexp/no-useless-flag: ["error", { "ignore": ["s", "g"] }] */

/* ✓ GOOD */
var foo = /\w/s;
/foo/g.test(string);

/* ✗ BAD */
var foo = /\w/i;
var foo = /\w/m;

"strictTypes": false

/* eslint regexp/no-useless-flag: ["error", { "strictTypes": false }] */

/* ✓ GOOD */
const notStr = {}
notStr.split(/foo/g);

/** @param {object} obj */
function fn1 (obj) {
    obj.search(/foo/g);
}

/* ✗ BAD */
maybeStr.split(/foo/g); // The type of "maybeStr" cannot be tracked.

function fn2 (maybeStr) {
    maybeStr.search(/foo/g);
}

❤️ Compatibility

This rule is compatible with clean-regex/no-unnecessary-flag rule.

🚀 Version

This rule was introduced in eslint-plugin-regexp v0.9.0

🔍 Implementation