在這種配置下我們要實現關鍵詞不區分大小寫搜索並高亮顯示要借助ASP的正則處理了,請看下面代碼:
復制代碼 代碼如下:
<%
Function Takeout(patrn,string1,colors)
'提取搜索關鍵字匹配文字
Dim regEx, Match, Matches, tt ' 建立變量。
Set regEx = New RegExp ' 建立正則表達式。
regEx.Pattern = patrn ' 設置模式。
regEx.IgnoreCase = True ' 設置是否區分大小寫。
regEx.Global = True ' 設置全局可用性。
Set Matches = regEx.Execute(string1) ' 執行搜索。
For Each Match in Matches ' 遍歷 Matches 集合。
RetStr = RetStr & Match.Value & " "
Next
RetStr = trim(RetStr)
if instr(RetStr," ")>0 then
for tt = 0 to ubound(split(RetStr," "))
string1 = replace(string1,split(RetStr," ")(tt),"<font color="""&colors&""">"&split(RetStr," ")(tt)&"</font>")
next
else
string1 = replace(string1,RetStr,"<font color="""&colors&""">"&RetStr&"</font>")
end if
Takeout = string1
End Function
response.write Takeout("jOeKOe", "Joekoe喬客雙語版","red")
Function Highlight(strContent,keyword) '標記高亮關鍵字
Dim RegEx
Set RegEx=new RegExp
RegEx.IgnoreCase =True '不區分大小寫
RegEx.Global=True
Dim ArrayKeyword,i
ArrayKeyword = Split(keyword," ")'用空格隔開的多關鍵字
For i=0 To Ubound(ArrayKeyword)
RegEx.Pattern="("&ArrayKeyword(i)&")"
strContent=RegEx.Replace(strContent,"<font color=red>$1</font>" )
Next
Set RegEx=Nothing
Highlight=strContent
End Function
response.write Highlight("Joekoe喬客雙語版","jOeKOe")
%>