asp 搜索关键词高亮显示并不区分大小写?

2025-03-18 17:45:56
推荐回答(1个)
回答1:

为你了这个问题,把我丢下几年的ASP又翻了一个遍,

明说吧,用replace是肯定不行的啦,因为它怎么都会替换上去,而你又不能因为你keyword的大小写,而变动替换的内容。

查了下资料,,也有人遇到这样的问题,

想到一个解决办法,用instr来就可以解决了。

从源字符串,自左向右搜索,每找到一个匹配项。就用来给关键词加上颜色,这样就不会进行替换了,一直循环到尾。

<%
public function Repcolor(str,keyword)
    dim Str_l,Str_m,Str_r,i
    Str_l=""
    Str_m=""
    Str_r=str
    i=instr(1,Str_r,keyword,1)
    do while i>0
        Str_l=Str_l & left(Str_r,i-1)
        Str_m=mid(Str_r,i,len(keyword))
        Str_l=Str_l & "" & Str_m & ""
        Str_r=right(Str_r,Len(Str_r)-len(keyword)-i+1)
        i=instr(1,Str_r,keyword,1)
    loop
    Repcolor=Str_l & Str_r
end function
s="abcaBcabCaBCabcaBCa"
r="bc"
response.Write s'输出原来字符串,跟下面高亮的对比,看看结果。
response.Write "
"'这里有个BR,百度可以过滤掉了,自己加上
response.Write Repcolor(s,r)
%>