ASP 正则操作函数封装
ASP 中也可以用正则表达式,为了方便使用,对正则表达式操作进行了封装,目前只封装了两个:
1、正则替换
'功能:通过正则表达式替换字符串
'参数: str 原字符串
' patton 正则匹配表达式
' restr 替换字符串
' op 选项 含有i忽略大小写,
' 含有g表示全局匹配,一般用法"ig"
'返回:替换后的字符串
'举例:regReplace("ss5ss6s7s","\d","","ig") 返回 ssssss
Public Function regReplace(str,patton,restr,op)
dim RegEx : Set RegEx = New RegExp
if instr(op,"i") > 0 then
RegEx.IgnoreCase =true
end if
if instr(op, "g") > 0 then
regEx.Global = true
end if
RegEx.Pattern = patton
regReplace=RegEx.replace(str,restr)
set RegEx = nothing
End Function
'参数: str 原字符串
' patton 正则匹配表达式
' restr 替换字符串
' op 选项 含有i忽略大小写,
' 含有g表示全局匹配,一般用法"ig"
'返回:替换后的字符串
'举例:regReplace("ss5ss6s7s","\d","","ig") 返回 ssssss
Public Function regReplace(str,patton,restr,op)
dim RegEx : Set RegEx = New RegExp
if instr(op,"i") > 0 then
RegEx.IgnoreCase =true
end if
if instr(op, "g") > 0 then
regEx.Global = true
end if
RegEx.Pattern = patton
regReplace=RegEx.replace(str,restr)
set RegEx = nothing
End Function
用法举例:如剔除一个字符串中所有的html标签
str = "dddffffdddd<script>ssssss</script><html></html><body></body> <></> <sss></sss>"
response.Write regReplace(str, "<(.*).+>.*<\/\1>", "","ig")
response.Write regReplace(str, "<(.*).+>.*<\/\1>", "","ig")
2、正则检测
'功能:通过正则表达式检测表达式是否包含字符串
'参数: str 原字符串
' patton 正则匹配表达式
' op 选项 含有i忽略大小写,
' 含有g表示全局匹配,一般用法"ig"
'返回:是否有匹配
'举例:regReplace("ss5ss6s7s","\d","ig") 返回 true
Public Function regTest(str,patton,op)
dim RegEx : Set RegEx = New RegExp
if instr(op,"i") > 0 then
RegEx.IgnoreCase =true
end if
if instr(op, "g") > 0 then
regEx.Global = true
end if
RegEx.Pattern = patton
regTest=RegEx.test(str)
set RegEx = nothing
End Function
用法举例:检测一个字符串中是否有html字符串
str = "dddffffdddd<script>ssssss</script><html></html><body></body> <></> <sss></sss>"
response.Write regReplace(str, "<(.*).+>.*<\/\1>", "ig")
response.Write regReplace(str, "<(.*).+>.*<\/\1>", "ig")