ASP中经常会用到replace,比如在查找时,要求高度显示被查找的字符,怎么不区分大小写替换字符?下面介绍两个方法,一个是利用replace 自身函数,另一种是用正则
方法一: 直接用ASP自带函数replace 这也是最简单的方法
title=replace(title,"DF","SD"1,-1,1)
replace函数参数详解:
参数1:源字符串
参数2:要被替换的字符
参数3:新的字符。,既,要将源字符串中的某些字符,替换成新指定的字符
参数4:值为1.指定从第一个字符开始搜索该字符串
参数5:值为-1 指定每一个子串都要被替换
参数6:值为1 指定字符串的比较不区分大小写。
(高亮显示关键字)的两个函数
方法2:用正则不区分大小写替换指定字符
以下是函数源代码:
<%
"下面也有两种方法,大家看情况使用:)
Function Takeout(patrn,string1,colors)
’提取搜索关键字匹配文字
Dim regEx, Match, Matches, tt ’ 建立变量。
Set regEx = New RegExp ’ 建立正则表达式。
regEx.Pattern = patrn ’ 设置模式。
regEx.IgnoreCase = True ’ 设置是否区分大小写。
regEx.Global = True ’ 设置全局可用性。
Set Matches = regEx.Execute(string1) ’ 执行搜索。
For Each Match in Matches ’ 遍历 Matches 集合。
RetStr = RetStr & Match.Value & " "
Next
RetStr = trim(RetStr)
if instr(RetStr," ")>0 then
for tt = 0 to ubound(split(RetStr," "))
string1 = replace(string1,split(RetStr," ")(tt),"
"&split(RetStr," ")(tt)&"")
next
else
string1 = replace(string1,RetStr,"
"&RetStr&"")
end if
Takeout = string1
End Function
response.write Takeout("NAkuO.cOM", "Nakuo.com囊括网络","red")
Function Highlight(strContent,keyword) ’标记高亮关键字
Dim RegEx
Set RegEx=new RegExp
RegEx.IgnoreCase =True ’不区分大小写
RegEx.Global=True
Dim ArrayKeyword,i
ArrayKeyword = Split(keyword," ")’用空格隔开的多关键字
For i=0 To Ubound(ArrayKeyword)
RegEx.Pattern="("&ArrayKeyword(i)&")"
strContent=RegEx.Replace(strContent,"
$1" )
Next
Set RegEx=Nothing
Highlight=strContent
End Function
response.write Highlight("Nakuo.com囊括网络","NAkuO.cOM")
%>