pageClass | sidebarDepth | title | description | since |
---|---|---|---|---|
rule-details |
0 |
regexp/no-useless-flag |
disallow unnecessary regex flags |
v0.9.0 |
plugin:regexp/recommended
config.
🔧 This rule is automatically fixable by the --fix
CLI option.
disallow unnecessary regex flags
This will point out present regex flags that do not change the pattern.
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;
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;
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;
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);
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);
No other flags will be checked.
{
"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
... Iftrue
, strictly check the type of object to determine if the regex instance was used insearch()
andsplit()
. Default istrue
. This option is only effective for verifying theg
andy
flags.
This option is always on when using TypeScript.
/* 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;
/* 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);
}
This rule is compatible with clean-regex/no-unnecessary-flag rule.
This rule was introduced in eslint-plugin-regexp v0.9.0