I've been experimenting with using HTML tags within TypeScript to highlight specific words in a sentence before displaying the sentence in HTML. Despite searching on StackOverflow for various solutions, I haven't been able to find one that works.
Here is an example of the code:
example.sentences = this.sentences.filter((sentence: { Sentence: string | string[]; }) => sentence.Sentence.includes(word))
.map((sentence: { Sentence: any; }) => sentence.Sentence.replace(new RegExp(word, 'gi'), match => {
return `<mark class="marking">${match}</mark>`;
}));
Then I display these sentences in HTML:
<mark *ngFor="let sentence of example.sentences">
{{ sentence }}
</mark>
The output looks like this:
Alice is in <mark class="marking">wonderland</mark>
Although it appears as text within the sentence, it's not rendering as HTML.
Is there a way to define this in TypeScript or in HTML in order to change the color of a word within a sentence?