我正在 Node 中创建一个包来解析和操作 csv,并需要使用 String.matchAll()
,但我收到一条错误消息,指出 str.matchAll 不是函数
。我尝试切换到 str.match()
并得到相同的错误。我尝试分别console.log 和都返回未定义。我在 Visual Studio Code PowerShell 中输入 node -v
并输出 v10.16.3
我的代码
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;
}
}
在标记的行处,它抛出错误 this.text 不是函数
我 console.logged typeof(this.text)
它给出了字符串,所以我不'不知道发生了什么
最佳答案
String.matchAll 仅从 Node.js 12.0 开始可用(请参阅此处的兼容性: string.matchAll )。
但是,String.match 在 Node.js 的早期版本中应该可用。
这是我创建的一个实际示例(Node v10.16.0):https://repl.it/repls/PunctualRareHypotenuse
我建议还确保有问题的对象是一个字符串,只是为了确定!
此外,如果您可以轻松升级,请尝试安装Node.js 12 .
代码:
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var regexp = /[A-E]/g;
var matches_array = str.match(regexp);
console.log(matches_array);
关于javascript - String.matchAll 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58558257/