'Does Node.js support the String.MatchAll method?

When I test the code:

let result = 'heymama'.matchAll(/m(a)/g);

I get the error "'heymama'.matchAll is not a function"

When I run the version:

let result = 'heymama'.match(/ma/g);

There's no error.



Solution 1:[1]

#String.matchAll is supported in Node.js from version 12.0.0

Check out the compatibility on MDN.

enter image description here

Solution 2:[2]

matchAll is available in Node.js starting from v12.0.0

Solution 3:[3]

Before Node 12:

As @dennis-vash alredy pointed out, it's currently not supported in Node.js.

There is this "match-all" npm package alternative though.

const matchAll = require("match-all");

let s = "Hello _World_ and _Mars_";
console.log(matchAll(s, /_([a-z]+)_/gi).toArray());
// => [ "World", "Mars" ]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Pier-Luc Gendreau
Solution 3 Lee Goddard