matchAll Throws error when g flag is missing now?

David Fann :

From the MDN docs it states

matchAll only returns the first match if the g flag is missing.

But if you run this code:

const regexp = RegExp('[a-c]', '');
const str = 'abc';
console.log(Array.from(str.matchAll(regexp), m => m[0]));
// Array [ "a" ]

in a recent version of chrome you get the following error:

Error: undefineds called with a non-global RegExp argument

I'm on chrome version 80.0.3987.116

I'm not sure if this is a chrome issue or the MDN documentation needs to be updated.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll

zfrisch :

The information presented to you on MDN, in this instance, is wrong.

The Official ECMA Specification, ECMA-262 as of February 15, 2020( See here ), states that if there is no g flag present, that matchAll should throw a TypeError.

See specifically 2.b.iii below.


matchAll MDN


The error message is obviously fairly confusing with its phrasing, but still correct.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11909&siteId=1