当前位置: 移动技术网 > IT编程>开发语言>.net > 实现搜索结果的关键词变色标注的程序

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

2019年01月11日  | 移动技术网IT编程  | 我要评论

sswyt,新浪轻博客,金岳霖 林徽因

<%
以前写全文检索程序的时候写的.
原创 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
  
%>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网