日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

html 显示搜索结果,搜索结果高亮显示(不改变html标签)

發(fā)布時(shí)間:2025/3/20 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html 显示搜索结果,搜索结果高亮显示(不改变html标签) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、問題的產(chǎn)生 搜索結(jié)果高亮顯示,在新聞標(biāo)題,來源之類的地方好做,只需要用str.Replace(keyword,"font style=/"color:red;/"" + keyword +"/font");這樣的方法就可實(shí)現(xiàn)。 例如:新聞內(nèi)容里的圖片img alt="" src="Upload/1.jpg" /,搜索時(shí)使用的關(guān)鍵字為oa

一、問題的產(chǎn)生

搜索結(jié)果高亮顯示,在新聞標(biāo)題,來源之類的地方好做,只需要用str.Replace(keyword,"");這樣的方法就可實(shí)現(xiàn)。

例如:新聞內(nèi)容里的圖片,搜索時(shí)使用的關(guān)鍵字為oa,則會(huì)將新聞內(nèi)容中這張圖片替換為oad/1.jpg" />,這張圖片就顯示不出來了。

二、實(shí)現(xiàn)原理

再次,將搜索結(jié)果替換為高亮顯示狀態(tài);

三、實(shí)現(xiàn)代碼

以下代碼是網(wǎng)上搜集的,來源不詳,在此對(duì)作者表示感謝。

public static string Reg(string input, string replace)

{

//設(shè)置高亮樣式

string replaceformat = "";

System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(String.Format("{0}", replace), System.Text.RegularExpressions.RegexOptions.Multiline);

return reg.Replace(input, string.Format(replaceformat, replace));

}

///

/// 設(shè)置各個(gè)html部分為自定義標(biāo)號(hào)

///

/// 輸入內(nèi)容

/// 匹配集合

/// 替換的字串

public static string protectHtml(string input, ref System.Text.RegularExpressions.MatchCollection matches)

{

//匹配html的正則

System.Text.RegularExpressions.Regex htmlReg =

new System.Text.RegularExpressions.Regex(@"/<.>", System.Text.RegularExpressions.RegexOptions.Multiline);

//獲取匹配集合

matches = htmlReg.Matches(input);

//設(shè)置替換字串

string markFormat = "[[{0}]]";

//替換html,記錄位置

for (int i = 0; i < matches.Count; i++)

{

input = input.Replace(matches[i].Value, string.Format(markFormat, i));

}

return input;

}

///

/// 將標(biāo)號(hào)恢復(fù)html

///

/// 高亮設(shè)置好的字串

/// 匹配集合

/// 最終字串

public static string restoreHtml(string input, System.Text.RegularExpressions.MatchCollection matches)

{

//設(shè)置替換字串

string markFormat = "[[{0}]]";

for (int i = 0; i < matches.Count; i++)

{

input = input.Replace(string.Format(markFormat, i), matches[i].Value);

}

return input;

}

使用方法:

System.Text.RegularExpressions.MatchCollection matches = null;

temp = Reg(temp, KeyWords);//替換關(guān)鍵字為高亮顯示

總結(jié)

以上是生活随笔為你收集整理的html 显示搜索结果,搜索结果高亮显示(不改变html标签)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。