Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

javascript - find and replace typescript angular

I use a dropdown which when a value gets selected sits as a word on the content editor.I am trying to find and replace words from the content editor. I was able to replace but at some scenarios it doesn't work.

I am trying to replace city to the name Newyork For example from the drop down values if I choose city in my console log the content editor content shows it as Newyork

So at first it works but if I choose other dropdown values and once again when I choose city it doesn't work

For example try to choose City first Name second and City again you will see that the second time when you chose city the console log value for city didnt change as Newyork

Please help if u guys know

changeValue() is the function name

Stackblitz: https://stackblitz.com/edit/angular-summernote-demo-ej4xpg?file=src%2Fapp%2Fapp.component.ts

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I did something(but not sure why are using regex her) now it is working as expected for your quetsion.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...