欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

实现搜索结果的关键词变色标注的程序

程序员文章站 2022-04-20 22:04:12
<% 以前写全文检索程序的时候写的. 原创 by 飞鸟@dev-club.com email: flybird@dev-club.com ie5.5 脚本引擎 req...
<%
以前写全文检索程序的时候写的.
原创 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
  
%>