当前位置:首页 > ASP教程 > 应用技巧

Highlight patterns within strings

复制代码 代码如下:

'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


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!