Highlight patterns within strings
程序员文章站
2024-01-20 21:33:34
复制代码 代码如下:'replaces pattern with highlighted replacement (usi...
复制代码 代码如下:
'replaces pattern with highlighted replacement (using style) and preserves case
public function highlight(strtext, strfind)
dim objregexp, i, strhighlight
'split the search terms into an array
dim arrfind
arrfind = split(strfind, " ")
'initialize the regular expression object to perfom the search
dim omatches, smatch
set oregexp = new regexp
oregexp.global = true 'returns all matches to the search term
oregexp.ignorecase = true 'case insensitive
'loop through the array of search terms to find matches
for i = 0 to ubound(arrfind)
oregexp.pattern = arrfind(i) 'sets the search pattern string
set omatches = oregexp.execute(strtext) '// performs the search
for each match in omatches
'build the code to be used to highlight results
strhighlight = "<span class=""highlight"">" & match.value & "</span>"
next
'replace matches from the search with the above code
strtext = oregexp.replace(strtext, strhighlight)
next
highlight = strtext
set objregexp = nothing
end function
上一篇: eclipse部署tomcat服务器无法启动问题的解决方法
下一篇: javaSE