String.prototype.matchAll() should be called with a global regular expression

  • BAD_MATCH_ALL_ARG
  • Error
  • High
  • es11

This rule applies when a regular expression without the global flag (g) is used at String.prototype.matchAll().

Unlike other methods using regular expression, String.prototype.matchAll() always finds all matches in the input string and returns an iterator of the matches including the capturing groups. So, it is nonsensical to use a regular expression without the global flag at String.prototype.matchAll(), which causes a TypeError exception.

Noncompliant Code Example

View with compliant examples side by side
const line = "x:0, y:42";
let numberMatches = line.matchAll(/\d+/); // BAD_MATCH_ALL_ARG alarm

Compliant Code Example

View with noncompliant examples side by side
const line = "x:0, y:42";
let numberMatches = line.matchAll(/\d+/g);

Version

This rule was introduced in DeepScan 1.44.0.

See

Was this documentation helpful?