收藏本页
联系我们
论坛帮助

牛头网络技术论坛程序员专区asp讨论 → asp中replace不区分大小写替换实现方法

垃圾帖子的防止方法 动网水印问题 sql里的字符替换方法 2003安全设置大全 短信检测提交问题

  共有182人关注过本帖树形打印

主题:asp中replace不区分大小写替换实现方法

帅哥,在线噢!
牛头

  1楼


头衔:站长
金币:5059
经验:6148
金钱:9145
等级:管理员
文章:572
精华:10
加为好友
发送短信
个性首页
asp中replace不区分大小写替换实现方法  发贴心情 Post By:2008-10-31 15:07:23 [只看该作者]

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")
%>


在线咨询技术 [牛头][早起的鸟儿有虫吃!]
 高速双线主机 支持(0中立(0反对(0回到顶部