当前位置: 首页 > 图文教程 > 网络编程 > ASP > 实现搜索结果的关键词变色标注的程序

ASP
用ASP获取客户端IP地址的方法
IIS服务器防范攻击3条安全设置技巧
ASP上传组件AspUploadv3.0安装的问题
ASP教程:读取文件和写文件
ASP实例教程:长文章分页功能
最常用的正则表达式示例
asp实现文件md5
ASP教程:DateDiff函数和DateAdd函数
ASP教程:nchar,char,varchar和nvarchar
ASP过滤script脚本的正则函数
ASP函数:移除HTML代码
ASP教程:800a0400的解决方案
ASP视频教程:Windows XP下安装IIS
ASP视频教程:Windows XP下配置IIS
ASP视频教程:安装SQL Server 2000数据库
ASP视频教程:SQL企业管理器建立数据库和管理员表
ASP视频:Dreamweaver中建立站点和连接数据库
ASP视频教程:建立后台管理的登录页面
ASP视频教程:对登录页面进行美化
ASP视频教程:建立后台管理的框架页面

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 49 ::
收藏到网摘: n/a

<%
' 以前写全文检索程序的时候写的.
' 原创 by 飞鸟@dev-club.com
' Email: [email protected]
' 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
  
%>