页面导航: 首页网络编程ASP编程应用技巧 → 正文内容

Highlight patterns within strings

发布:dxy 字体:[增加 减小] 类型:转载


'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

百度中搜索更多的关于Highlight patterns within strings内容,或者用Google搜索相关更多
浏览次数:载入中... 打印本文关闭本文返回首页

文章评论

共有 位脚本之家网友发表了评论我来说两句

同 类 文 章
最 近 更 新
热 点 排 行