Skip to content

Latest commit

 

History

History
73 lines (50 loc) · 2.29 KB

no-missing-g-flag.md

File metadata and controls

73 lines (50 loc) · 2.29 KB
pageClass sidebarDepth title description since
rule-details
0
regexp/no-missing-g-flag
disallow missing `g` flag in patterns used in `String#matchAll` and `String#replaceAll`
v1.10.0

regexp/no-missing-g-flag

💼 This rule is enabled in the ✅ plugin:regexp/recommended config.

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

disallow missing g flag in patterns used in String#matchAll and String#replaceAll

📖 Rule Details

When calling String#matchAll() and String#replaceAll() with a RegExp missing the global (g) flag, it will be a runtime error. This rule reports RegExps missing the global (g) flag that cause these errors.

/* eslint regexp/no-missing-g-flag: "error" */
const games = 'table football, foosball'
const text = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

/* ✓ GOOD */
var m = games.matchAll(/foo/g);
var newText = text.replaceAll(/Dog/ig, 'cat');


/* ✗ BAD */
var m = games.matchAll(/foo/);
var newText = text.replaceAll(/Dog/i, 'cat');

🔧 Options

{
  "regexp/no-missing-g-flag": ["error",
    {
      "strictTypes": true
    }
  ]
}
  • strictTypes ... If true, strictly check the type of object to determine if the regex instance was used in matchAll() and replaceAll(). Default is true.
    This option is always on when using TypeScript.

📚 Further reading

🚀 Version

This rule was introduced in eslint-plugin-regexp v1.10.0

🔍 Implementation