欢迎光临
我们一直在努力

实现搜索结果的关键词变色标注的程序(使用正则表达式)

建站超值云服务器,限时71元/月

<%
以前写全文检索程序的时候写的.
原创 by 飞鸟@dev-club.com
email: flybird@dev-club.com
ie5.5 脚本引擎 required

    dim patern
    dim found
    
    dim str
    dim result
    
    patern="(a)|(b)"
    str=" a dog fall in love with a cat. can you believe?"
    result=""    
    call getmatchtext(str,result,false)
    response.write result

    sub getmatchtext(byref str,byref result,isneedtrunc)
        on error resume next
        dim regex, match, matches
        dim tstr
        set regex = new regexp          建立正则表达式。        
        regex.pattern = (patern)     设置模式。
        regex.ignorecase = true          设置是否区分字符大小写。
        regex.global = true          设置全局可用性。
        set matches = regex.execute(str)    执行搜索。    
        if err.number<>0 then
            response.write "错误1:" & err.description
            err.clear
            exit sub
        end if
        if matches.count <>0 then
            dim startindex            
            dim mymatchvalue
            startindex=1
            for each match in matches
                if (instr(str,match.value)>0) then
                    if instr(str,match.value)-50 >0 then
                        startindex=instr(str,match.value)-50
                    else
                        startindex=1
                    end if
                    mymatchvalue=match.value
                    exit for
                end if
            next
            if isneedtrunc then
                result= (mid(str,startindex,strlength(mymatchvalue)+100))
            else
                result= (str)    
            end if
            for each match in matches
                if not(instr(result,"<font color=red>" & match.value & "</font>")>0) then
                    result=replace(result,match.value,"<font color=red>" & match.value & "</font>" )
                end if
            next
            found=true
        else
            found=false
        end if    
        set regex=nothing
    end sub
    
%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 实现搜索结果的关键词变色标注的程序(使用正则表达式)
分享到: 更多 (0)