[英]How to make sure searched text in a C# WebBrowser control is actual text and not an element or attributes?
如果有人仍然可以回答這個問題,我將在此處保留,但我將走另一條路線進行搜索
我知道這里有幾個類似的問題,但是沒有一個讓我明白我要去的地方。
我的搜索部分基本完成。 它工作得很漂亮。 獲取所有出現的搜索詞或短語,忽略大小寫。 但是問題是,如果您要搜索“ div”或“表”或某個其他單詞(即html元素名稱或屬性值),則搜索會試圖突出顯示該單詞,從而完全破壞了頁面。
因此,我真的只需要一種簡單的方法來確保搜索忽略這些情況。 這就是我所擁有的。 我認為我可能需要一個非常好的正則表達式,但是我不能編寫一個正則表達式來挽救生命,因此我們將不勝感激。
private void PerformSearch()
{
string searchString = SearchTextBox.Text;
HtmlDocument doc = ManualViewBrowser.Document;
StringBuilder html = new StringBuilder(doc.Body.InnerHtml);
doc.Body.InnerHtml = Regex.Replace(html.ToString(), searchString, new MatchEvaluator(Highlight), RegexOptions.IgnoreCase);
}
private string Highlight(Match m)
{
return "<em class=\"highlight\">" + m.Value + "</em>";
}
只需使用以下方法從該html字符串中刪除所有html標簽:
private string RemoveHtmlTags(string html) {
return Regex.Replace(html, "<.*?>", String.Empty);
}
編輯:
您是對的,因此與其在html中進行搜索,還不如遍歷頁面的所有節點並在其中搜索單詞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.