javascript - 查找和替换打字稿角度
问题描述
我使用一个下拉菜单,当一个值被选中时,它会作为内容编辑器上的一个单词出现。我正在尝试从内容编辑器中查找和替换单词。我可以更换,但在某些情况下它不起作用。
我正在尝试将城市替换为名称 Newyork 例如,如果我在控制台日志中选择城市,则从下拉值中,内容编辑器内容将其显示为 Newyork
所以起初它可以工作,但如果我选择其他下拉值,当我再次选择城市时,它就不起作用
例如,尝试选择 City first Name second 并再次选择 City,您将看到第二次选择 city 时,city 的控制台日志值没有更改为 Newyork
如果你们知道请帮忙
changeValue() 是函数名
Stackblitz:https ://stackblitz.com/edit/angular-summernote-demo-ej4xpg?file=src%2Fapp%2Fapp.component.ts
解决方案
我做了一些事情(但不确定她为什么要使用正则表达式),现在它可以按预期工作。
changeValue(ev) {
const regex = /(<\/p>)$/gm;
if (regex.test(this.textValue)) {
this.textValue = this.textValue.replace(regex, "");
this.textValue += ` ${ev.value}</p>`;
} else {
this.textValue += ` ${ev.value}`;
}
let cleanText = this.textValue.replace(/<\/?[^>]+(>|$)/g, "");
let abc = cleanText.replace(/ /g, "");
let final = abc.replace("City", "Newyork");
this.textValue = final;
console.log("textValue", final);
}
}
https://stackblitz.com/edit/angular-summernote-demo-tauve8?file=src%2Fapp%2Fapp.component.ts
推荐阅读
- winapi - 内存不足 WinAPI
- android - 未在 AlarmManager onReceive() 方法中获取用户定义的 Arraylist
- python - 使用条件等级字母排序
- graphql - 为什么当父解析器返回 null/undefined 时,graphql-js 执行器停止解析具有自己解析器的子字段?
- javascript - 如何让我的滑块显示在缩小的同时保护其纵横比?
- r - 创建一个 R 函数:所有部件都工作,组装时失败
- python - 如何在不退出 python 脚本的情况下销毁一个 QApplication 然后运行一个新的?
- api - 来自 k8s 主节点的 Cluster-IP 连接受损/延迟
- database-design - 表单上的 MS Access 文本框是四舍五入的数字
- android - 使用 Kotlin 使用 doOntextListener 将焦点切换到 maxlength 之后的下一个编辑文本