我想对字符串应用正则表达式。为了获取所有组的结果,我正在使用matchAll方法。这是我的代码
const regexp = RegExp('foo*','g');
const str = "table football, foosball";
let matches = str.matchAll(regexp);
for (const match of matches) {
console.log(match);
}
在编译上面的代码时,我遇到了错误:
类型“"table football, foosball"”上不存在属性“matchAll”。
在搜索这个错误时,我在stackoverflow上找到了类似的问题:
TS2339:类型“string”上不存在属性“includes”
我按照上述链接中提到的更改了tsconfig配置,但我的问题没有解决。
这是我的tsconfig代码:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"importHelpers": true,
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2016",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
}
}
matchAll is not a function
的错误,这可能是因为你正在使用 TypeScript 作为类型检查器,并且同时使用其他工具(例如 webpack + babel)作为转译器。因此,你仍然需要找到一种方法来填充 matchAll 函数。 - Jay Wick"lib": ["es2020.string"]
时遇到了一些非常奇怪的行为。 使用"target": "es2020"
效果更好。 此外,不知何故,尽管vsc声称正在运行v15,但必须在launch.json中更改节点版本,因为节点没有matchAll。 - Tamir Daniely