查找并替换打字稿角度

问题描述

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

我正在尝试将城市替换为 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(/&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