ASP 100分求完整正则表达式

mysite365 2010-06-04 12:35:11

<a href="ftp://www.163.com/11.asp?id=111&id2=333">链接1</a>
<a href="http://www.163.com/11.asp?id=111&id2=333">链接2</a>
<a href="www.163.com/11.asp?id=111&id2=333">链接3</a>

用正则提取上面超链接地址,分别替换成

ftp://www.163.com/11.asp?id=111&id2=333 替换为 http://www.111.com/111.asp?url=ftp://www.163.com/11.asp?id=111&id2=333
http://www.163.com/11.asp?id=111&id2=333 替换为 http://www.111.com/111.asp?url=http://www.163.com/11.asp?id=111&id2=333
www.163.com/11.asp?id=111&id2=333 替换为 http://www.111.com/111.asp?url=http://www.163.com/11.asp?id=111&id2=333


下面我写的,求找错修正

Function ReplaceUrl(Url,str)
Dim reg , resultStr
if isNull(Url) then
ReplaceUrl = null
Exit Function
end if
Set reg=new RegExp
'reg.Pattern = "(http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)"
re.Pattern="([^(http:\/\/)])(www\.([\w-]+\.)+[\w]+(\/[\w-]+)*[\/]?([\w-]+\.[\w]+)?(\?[\w]+=[\w]+(&[\w]+=[\w]+)*)?)"
reg.Global = True '搜索匹配字串针对全部文本
re.IgnoreCase=true '忽略大小写

resultStr = reg.replace(Url,str"$1")

Set reg=nothing
ReplaceUrl = resultStr
End Function

url="<a href="http://www.163.com/11.asp?id=111&id2=333">链接1</a>"

reResponse.write ReplaceUrl(url,"http://123.com/asp.asp?url=")

%>
...全文
133 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
z109876543210 2010-06-04
  • 打赏
  • 举报
回复
或者:

reg.Pattern="\s+href=(""*)"
reg.Global = True '搜索匹配字串针对全部文本
reg.IgnoreCase=true '忽略大小写

resultStr = reg.replace(Url, " href=$1" & str)
z109876543210 2010-06-04
  • 打赏
  • 举报
回复

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Function ReplaceUrl(Url,str)
Dim reg , resultStr
if isNull(Url) then
ReplaceUrl = null
Exit Function
end if
Set reg=new RegExp
'reg.Pattern = "(http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)"
reg.Pattern="href=(""*)(http://|ftp://|www\.)"
reg.Global = True '搜索匹配字串针对全部文本
reg.IgnoreCase=true '忽略大小写

resultStr = reg.replace(Url, "href=$1" & str & "$2")

Set reg=nothing
ReplaceUrl = resultStr
End Function

url="<a href=""ftp://www.163.com/11.asp?id=111&id2=333"">链接1</a><a href=ftp://www.778899.com/11.asp?id=111&id2=333>链接1</a><a href=""http://www.163.com/11.asp?id=111&id2=333"">链接2</a><a href=""www.163.com/11.asp?id=111&id2=333"">链接3</a>"

'response.write url
'response.write "<br >"
response.write ReplaceUrl(url,"http://123.com/asp.asp?url=")

%>
mocom 2010-06-04
  • 打赏
  • 举报
回复
.Pattern = "(?!ftp|http)<a href=""([^.""]+)\.([^""]+)"">"
改成
.Pattern = "(?!ftp|http)<a href=""([^\.""]+)\.([^""]+)"">"
忘记加转义了
mocom 2010-06-04
  • 打赏
  • 举报
回复

Dim tmp
tmp = "<a href=""ftp://www.163.com/11.asp?id=111&id2=333"">链接1</a>" & vbCrLf & _
"<a href=""http://www.163.com/11.asp?id=111&id2=333"">链接2</a>" & vbCrLf & _
"<a href=""www.163.com/11.asp?id=111&id2=333"">链接3</a>"

With New RegExp
.Pattern = "<a href=""(ftp|http)\://([^""]+)"">"
.Global = True
.IgnoreCase = True
tmp = .Replace(tmp, "<a href=""http://www.111.com/111.asp?$1://$2"">")
.Pattern = "(?!ftp|http)<a href=""([^.""]+)\.([^""]+)"">"
.Global = True
.IgnoreCase = True
tmp = .Replace(tmp, "<a href=""http://www.111.com/111.asp?http://$1.$2"">")
End With
WScript.Echo tmp

  • 打赏
  • 举报
回复
Function ReplaceUrl(Url,str)
Dim reg , resultStr,restr
if isNull(Url) then
ReplaceUrl = null
Exit Function
end if
Set reg=new RegExp
re.Pattern="href=[""']*(http:\/\/)?([^""']+)[""']*|href=[""']*(ftp:\/\/)([^""']+)[""']*"
reg.Global = True '搜索匹配字串针对全部文本
re.IgnoreCase=true '忽略大小写
restr=reg.replace(Url,"$3")
if restr="ftp://" then
resultStr = reg.replace(Url,str"href=$3$4")
else
resultStr = reg.replace(Url,str"href=http://$2")
end if
Set reg=nothing
ReplaceUrl = resultStr
End Function

url="<a href=""http://www.163.com/11.asp?id=111&id2=333"">链接1</a>"

Response.write ReplaceUrl(url,"http://123.com/asp.asp?url=")

随手写的,你自己测试吧

28,409

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧