这是一个TypeScript方法,它想要遍历一个字符串数组,并返回另一个字符串数组,其中匹配正则表达式(格式类似于“[la la la]”)的字符串将变为“la la la”,不匹配的字符串将被删除。因此,如果我的输入数组是:
我得到了这样的输出结果:
"[x]", "x", "[y]"
它变成了
"x", "y"
以下是我的代码:
questions(): string[] {
var regexp = /\[(.*)\]/;
return this.rawRecords[0].map((value) => {
console.log(value);
var match = regexp.exec(value);
if (match) {
return match[1];
}
});
}
我得到了这样的输出结果:
"x", undefined, "y"
由于存在 "if (match)",请问应该如何正确编写此代码的typescript/javascript版本?
.filter(Boolean)
来除去未定义的值。 - elclanrs