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

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

相关推荐:

为什么Python脚本在任务计划程序中由于路径问题无法运行?

根本原因是Windows任务计划程序默认工作目录为C:\Windows\System32,导致相对路径失效;需强制切换工作目录、使用绝对路径、配置完整Python解释器路径、显式添加环境变量、重定向日志并启用历史记录。 任务计划程序中Python脚本找不到模块或文件 根本原因不是Python本身出错...

如何在不使用正则表达式的情况下按关键词“shiftdate”分割字符串

本文介绍一种纯Python内置方法,无需导入re模块,即可将长字符串按指定关键词(如"shiftdate")切分为多个子串,并确保每个子串以该关键词开头。核心思路是利用str.split()配合字符串拼接,精准保留分隔符。 本文介绍一种纯python内置方法,无需导入`re`模块,即可将长字符串按指...

为 NumPy 数组子类正确标注 __getitem__ 方法的类型提示

NumPy的ndarray子类在运行时能保持类型(如切片返回原subclass实例),但静态类型检查器(如Pyright)因缺乏泛型返回类型推导而报错;需手动重写__getitem__并使用Self或协变泛型确保类型安全。 numpy的`ndarray`子类在运行时能保持类型(如切片返回原subcl...

如何优雅地捕获并处理 pandas.concat 的异常以继续执行程序

本文介绍在使用pandas读取多个csv文件并合并时,如何正确捕获空文件等导致的异常,避免程序中断,同时保留日志反馈和流程可控性。 本文介绍在使用pandas读取多个csv文件并合并时,如何正确捕获空文件等导致的异常,避免程序中断,同时保留日志反馈和流程可控性。 在批量处理CSV元数据文件(如met...