Skip to content

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

Now loading...

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

Now loading...

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

Now loading...

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.

Now loading...

y flag (sticky)

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

Now loading...

other flags

No other flags will be checked.

🔧 Options

json5
{
  "regexp/no-useless-flag": ["error",
    {
      "ignore": [], // An array of "i", "m", "s", "g" and "y".
      "strictTypes": true
    }
  ]
}
{
  "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"]

Now loading...

"strictTypes": false

Now loading...

❤️ 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