Javascript String.matchAll未定义
我正在节点中创建一个包来解析和操作csv,需要使用Javascript String.matchAll未定义,javascript,node.js,visual-studio-code,Javascript,Node.js,Visual Studio Code,我正在节点中创建一个包来解析和操作csv,需要使用String.matchAll(),我收到一个错误,说str.matchAll不是函数。我尝试切换到str.match(),但得到了相同的错误。我尝试了console.log,但都返回未定义。我在VisualStudioCodePowerShell中键入了node-v,它输出了v10.16.3 我的代码 fs = require('fs'); class Table { //the function I needed it for
String.matchAll()
,我收到一个错误,说str.matchAll不是函数。我尝试切换到str.match()
,但得到了相同的错误。我尝试了console.log,但都返回未定义。我在VisualStudioCodePowerShell中键入了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;
}
}
fs=require('fs');
类表{
//我需要它的功能
清除器(标题、值){
让regLine=“”;
for(让i=0;iString.matchAll仅在Node.js 12.0之后可用(请参见此处的兼容性:)
但是,String.match应该可以从Node.js的早期版本获得
下面是我创建的一个实例(节点v10.16.0):
我还建议确保所讨论的对象是一个字符串,以便确定
另外,如果您很容易升级,请尝试安装
代码:
var str='abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz';
var regexp=/[A-E]/g;
var matches_array=str.match(regexp);
console.log(匹配\u数组)
你能给我们看一下你的代码吗?你确定str是字符串吗?你如何设置它的值?Occam的剃刀:这个。text
不是字符串。@JaredSmith它肯定是字符串,所以你可能只需要升级你的Node.js安装,或者使用一个变通方法。Node 12现在在LTS上,所以应该可以安装。@Mat如果它没有match
属性,它就不是字符串。string.prototype.match
从1999年就开始了。你在Netscape Navigator中测试它吗?@JaredSmith在尝试排除故障时,我把console.log(typeof(this.text))
放在this.text.match(regex)之前
并且它记录了字符串。我正在vs代码中运行东西