Asp高手进:正则表达式替换内容

来源:百度知道 编辑:UC知道 时间:2024/05/18 18:59:44
<!--开始-->这里第一个需要替换的内容<!--结束-->
不需要替换的内容
<!--开始-->这里第二个需要替换的内容<!--结束-->
asdas d asd as d as d asd as d asd as
<!--开始-->这里第三个需要替换的内容<!--结束-->
<!--开始-->这里第四个需要替换的内容<!--结束-->
自行车自行车形成子形成在才这些才在

★★★★★★★★★★以上是需要替换的内容-content
需要替换的内容要求:<!--开始-->……<!--结束-->
替换成:被替换了
说明:content中需要替换的记录数未知,共同点是开始部分和结束部分一致,中间不一致,要求用正式式写一个函数进行替换,并且能够统计需要替换的内容一共有几条

<%
content = "<!--开始-->这里第一个需要替换的内容<!--结束--> 不需要替换的内容 <!--开始-->这里第二个需要替换的内容<!--结束--> asdas d asd as d as d asd as d asd as <!--开始-->这里第三个需要替换的内容<!--结束--> <!--开始-->这里第四个需要替换的内容<!--结束--> 自行车自行车形成子形成在才这些才在"

'正则替换函数'
Function ReplaceExp(srcstr, patrn, replStr)
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
regEx.Execute(srcstr)
ReplaceExp = regEx.Replace(srcstr, replStr)
Set regEx = Nothing
End Function

'调用并输出'
Response.write content
Response.write "<hr>"
Response.write ReplaceExp(content,"\<\!\-{2}开始\-{2}\>.*?\<\!\-{2}结束\-{2}\>","被替换了")
%>

regreplace(str,regexstr,repstr) 对str 进行正则替换
repstr为要替换成的内容

如:

htmlstr = "123<img src="Asp高手进:正则表达式替换内容 ASP高手进 正则表达式 asp 正则表达式替换问题 ASP高手进,一个正则表达式 正则表达式替换,高手帮帮忙呀 正则表达式替换问题 正则表达式 替换 asp 正则表达式 替换HTML源文件里的链接地址 php正则表达式替换问题 怎样用正则表达式替换str_replace