# regexp/no-useless-flag

disallow unnecessary regex flags

  • ⚙️ This rule is included in "plugin:regexp/recommended".
  • 🔧 The --fix option on the command line (opens new window) can automatically fix some of the problems reported by this rule.

# 📖 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

{
  "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 (opens new window) rule.

# 🚀 Version

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

# 🔍 Implementation

Last Updated: 6/25/2022, 12:32:38 PM