实现搜索结果的关键词变色标注的程序

 1   
 2' 以前写全文检索程序的时候写的.   
 3' 原创 by 飞鸟@dev-club.com   
 4' Email: [email protected]   
 5' ie5.5 脚本引擎 required   
 6  
 7dim patern   
 8dim found   
 9  
10dim str   
11dim result   
12  
13patern="(a)|(b)"   
14str=" A dog fall in love with a cat. Can you believe?"   
15result=""   
16call getMatchText(str,result,false)   
17Response.Write result   
18  
19sub getMatchText(byref str,byref result,isNeedTrunc)   
20'on error resume next   
21Dim regEx, Match, Matches   
22dim tStr   
23Set regEx = New RegExp ' 建立正则表达式。   
24regEx.Pattern = (patern) ' 设置模式。   
25regEx.IgnoreCase = True ' 设置是否区分字符大小写。   
26regEx.Global = True ' 设置全局可用性。   
27Set Matches = regEx.Execute(str) ' 执行搜索。   
28if err.number<>0 then   
29response.write "错误1:" & err.description   
30err.clear   
31exit sub   
32end if   
33if matches.count <>0 then   
34dim startIndex   
35dim myMatchValue   
36startIndex=1   
37for each match in matches   
38if (instr(str,match.value)>0) then   
39if instr(str,match.value)-50 >0 then   
40startIndex=instr(str,match.value)-50   
41else   
42startIndex=1   
43end if   
44myMatchValue=match.value   
45exit for   
46end if   
47next   
48if isNeedTrunc then   
49result= (mid(str,startIndex,strLength(myMatchValue)+100))   
50else   
51result= (str)   
52end if   
53for each match in matches   
54if not(instr(result,"

<font color="red">" &amp; match.value &amp; "</font>

1")>0) then   
2result=replace(result,match.value,"

<font color="red">" &amp; match.value &amp; "</font>

 1" )   
 2end if   
 3next   
 4found=true   
 5else   
 6found=false   
 7end if   
 8set regEx=nothing   
 9end sub   
10  
Published At
Categories with Web编程
Tagged with
comments powered by Disqus