我正在寻找一些解决方案,帮助从带有突出显示功能的 html 字符串中搜索术语。我可以通过从字符串中删除 html 内容来做到这一点。但问题是我将无法看到它突出显示的原始内容。我确实有以下功能,可以在没有 html 标记的情况下搜索和突出显示字符串。
private static updateFilterHTMLValue(value: string, filterText: string): string
{
if (value == null) {
return value;
}
let filterIndex: number = value.toLowerCase().indexOf(filterText);
if (filterIndex < 0) {
return null;
}
return value.substr(0, filterIndex)
+ "<span class='search-highlight'>"
+ value.substr(filterIndex, filterText.length)
+ "</span>"
+ value.substr(filterIndex + filterText.length, value.length - (filterIndex + filterText.length));
}
Run Code Online (Sandbox Code Playgroud)
因此,为了管理使用 html 对字符串的搜索,我创建了可以使用 html 搜索字符串的新函数。(我在搜索正确的字符串匹配之前删除了 html 部分)
private static test(value: string, filterText: string): string {
if (value == null) {
return value;
} …Run Code Online (Sandbox Code Playgroud)