Highlight patterns within strings

2018-09-06 10:30

阅读:781

  复制代码 代码如下:
Replacespatternwithhighlightedreplacement(usingstyle)andpreservescase
PublicFunctionhighlight(strText,strFind)
DimobjRegExp,i,strHighlight

Splitthesearchtermsintoanarray
DimarrFind
arrFind=Split(strFind,)

Initializetheregularexpressionobjecttoperfomthesearch
DimoMatches,sMatch
SetoregExp=NewRegExp

oregExp.Global=TrueReturnsallmatchestothesearchterm
oregExp.IgnoreCase=TrueCaseinsensitive

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


评论


亲,登录后才可以留言!