Если все возможные пользовательские теги известны, вы можете кодировать их перед передачей строки в привязку [innerHTML]
. Метод encodeCustomTags
в следующем фрагменте кода использует регулярное выражение для замены <customTag>
на <customTag>
:
private customTags = [
"CUSTOM_TAG",
"otherTag",
];
myHtml = this.encodeCustomTags("the <CUSTOM_TAG>boy</CUSTOM_TAG> went to the <b>store</b>");
private encodeCustomTags(html: string): string {
let regex: RegExp;
for (let tag of this.customTags) {
regex = new RegExp(`<(/?)${tag}>`, "gi");
html = html.replace(regex, `<$1${tag}>`)
}
return html;
}
См. этот стек для демонстрации.