javascript - 笑话:TypeError:replaceAll 不是函数
问题描述
String.prototype.replaceAll()
是一种有用的方法,在构建和执行时一切正常。但是,所有 Jest 测试都失败并出现以下错误:
TypeError: replaceAll is not a function
这些是我的依赖项:
"dependencies": {
"core-js": "^3.6.5",
"vue": "^2.6.11",
"vue-class-component": "^7.2.3",
"vue-i18n": "^8.22.0",
"vue-property-decorator": "^8.4.2",
"vue-router": "^3.3.4",
"vuex": "^3.5.1"
},
"devDependencies": {
"@vue/test-utils": "^1.1.0",
"jest-junit": "^12.0.0",
"ts-jest": "^26.4.1",
"typescript": "~3.9.3",
"vue-jest": "^3.0.7",
"vue-template-compiler": "^2.6.10"
},
我该如何解决这种行为?
解决方案
这很可能发生,因为String.prototype.replaceAll
没有在 Node.js 中实现(至少从 version 开始v14.15.0
)。
您可以使用的一种替代方法是正则表达式,如本例所示:
const str = 'foo-foo';
const regex = /foo/g; // Note the 'g' flag, which matches all occurrences of the expression
console.log(str.replace(regex, 'bar')); // 'bar-bar'
您可以在此处查看有关正则表达式的更多信息。
推荐阅读
- android - 如何创建具有透明背景和阴影的形状,但在形状轮廓后面不应该看到阴影?
- python - 使用线程模块对不同域的并行 GET 请求
- django - 如何使用 Django 将我的主页重定向到我的主域?
- spring-boot - 难以设置连接面项目以提供第一页
- java - 正确缩进 Java/Kotlin 控制台输出
- python - 如何在列表中逐步添加数字,同时从最低值到最高值排序?
- javascript - 从观察者回调调用Vue组件道具`undefined`
- sql-server - where 子句中空值的 case 表达式
- c# - 代码在 Windows 窗体应用程序中未正确执行
- swift - SceneKit 的外观(at:up:localFront:) 在某些情况下无法正常工作