Highlight patterns within strings

(编辑:jimmy 日期: 2024/9/22 浏览:2)

复制代码 代码如下:
'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

一句话新闻
Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。