复制代码 代码如下:
'Replacespatternwithhighlightedreplacement(usingstyle)andpreservescase
PublicFunctionhighlight(strText,strFind)
DimobjRegExp,i,strHighlight
'Splitthesearchtermsintoanarray
DimarrFind
arrFind=Split(strFind,"")
'Initializetheregularexpressionobjecttoperfomthesearch
DimoMatches,sMatch
SetoregExp=NewRegExp
oregExp.Global=True'Returnsallmatchestothesearchterm
oregExp.IgnoreCase=True'Caseinsensitive
'Loopthroughthearrayofsearchtermstofindmatches
Fori=0toUBound(arrFind)
oregExp.Pattern=arrFind(i)'Setsthesearchpatternstring
SetoMatches=oregExp.Execute(strText)'//performsthesearch
foreachmatchinoMatches
'Buildthecodetobeusedtohighlightresults
strHighlight="<spanclass=""highlight"">"&match.value&"</span>"
next
'Replacematchesfromthesearchwiththeabovecode
strText=oregExp.Replace(strText,strHighlight)
Next
highlight=strText
SetobjRegExp=Nothing
EndFunction