首页 > 解决方案 > 查找和替换打字稿角度

问题描述

我使用一个下拉菜单,当一个值被选中时,它会作为内容编辑器上的一个单词出现。我正在尝试从内容编辑器中查找和替换单词。我可以更换,但在某些情况下它不起作用。

我正在尝试将城市替换为名称 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

标签: javascripthtmlangulartypescriptsummernote

解决方案


我做了一些事情(但不确定她为什么要使用正则表达式),现在它可以按预期工作。

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(/&nbsp;/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


推荐阅读