28,409
社区成员




<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=")
%>
reg.Pattern="\s+href=(""*)"
reg.Global = True '搜索匹配字串针对全部文本
reg.IgnoreCase=true '忽略大小写
resultStr = reg.replace(Url, " href=$1" & str)
<%@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=")
%>
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=")