'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]
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 |