Ad

String.matchAll Is Undefined

- 1 answer

I am creating a package in node to parse and manipulate csv and needed to use String.matchAll() and I got an error saying str.matchAll is not a function. I tried switching to str.match() and got the same error. I tried to console.log each and both returned undefined. I typed node -v into the visual studio code powershell and it spat out v10.16.3

my code

fs = require('fs');

class Table {
    //the function I needed it for
    removeRow(header, value){
        let regLine = "";
        for(let i=0; i<this.colArr.length; i++){
            if (this.colArr[i][0]==header){
                regLine+=value+",";
            }else{
                regLine+=".*,"
            }
        }
        regLine = "\n"+regLine.substring(0,regLine.length-2)+"\n";
        let regex = new RegExp(regLine);

        let removed = this.text.matchAll(regex);//this line
        let newText = this.text.replace(regex,"\n");
        fs.writeFile(this.link, newText);
        this.update();
        return removed;
    }
}

At the line marked it throws the error this.text is not a function I console.logged typeof(this.text) and it gave string so I don't know whats going on

Ad

Answer

String.matchAll is only available from Node.js 12.0 onwards (see compatibility here: string.matchAll).

String.match however should be available from early versions of Node.js.

Here's an example I've created of it in action (Node v10.16.0): https://repl.it/repls/PunctualRareHypotenuse

I'd suggest also ensuring that the object in question is a string just to be sure!

Also if it's easy for you to upgrade, try installing Node.js 12.

Code:

var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var regexp = /[A-E]/g;
var matches_array = str.match(regexp);

console.log(matches_array);

Ad
source: stackoverflow.com
Ad