经典的asp/vbscript-使用正则表达式修改所有href [英] classic asp/vbscript - modify all hrefs with regex
问题描述
在经典ASP(VB脚本)中,我需要通过编码当前url并将其预先添加到字符串中来修改字符串中包含的多个不同的href.
In Classic ASP (VB Script), I need to modify multiple distinct hrefs that are contained in a string by encoding the current url and pre-pending to it.
基本上,我想让所有href都通过我的redirect.asp并传递编码到新链接中的现有href.
Basically, I want to make all hrefs go through my redirect.asp and pass in the existing href encoded into the new link.
例如:
已存在:
<a href="http://www.dairyqueen.com/us-en/Promotions-US/?localechange=1&test=1">
所需结果:
<a href="/redirect.asp?id=123&url=http%3A%2F%2Fwww.dairyqueen.com%2Fus-en%2FPromotions-US%2F%3Flocalechange%3D1%26test%3D1">
请注意,字符串中包含多个不同的href.所有这些都需要更换.
Note that there are multiple distinct href contained in the string. All of them need to be replaced.
此外,我还想在href中添加其他属性,除非有更好的方法,否则我可能只使用replace(myString,"<a href","<a target=""_blank"" href=")
即可完成此操作.
In addition, I would also like to add an additional attributes within the href as well, which I probably could do just using a replace(myString,"<a href","<a target=""_blank"" href=")
unless there is a better way.
最佳结果:
<a target="_blank" href="/redirect.asp?id=123&url=http%3A%2F%2Fwww.dairyqueen.com%2Fus-en%2FPromotions-US%2F%3Flocalechange%3D1%26test%3D1">
推荐答案
看看下面的代码:
' <a href="http://www.dairyqueen.com/us-en/Promotions-US/?localechange=1&test=1">
sHtml = "<a href=""http://www.dairyqueen.com/us-en/Promotions-US/?localechange=1&test=1"">"
Set refRepl = GetRef("fnRepl")
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "<a([\s\S]*?)href=""([\s\S]*?)""([\s\S]*?)>"
sResult = .Replace(sHtml, refRepl)
End With
' <a target="_blank" href="/redirect.asp?id=123&url=http%3A%2F%2Fwww.dairyqueen.com%2Fus-en%2FPromotions-US%2F%3Flocalechange%3D1%26test%3D1">
MsgBox sResult
Function fnRepl(sMatch, sSubMatch1, sSubMatch2, sSubMatch3, lPos, sSource)
fnRepl = "<a" & sSubMatch1 & "target=""_blank"" href=""/redirect.asp?id=123&url=" & EncodeUriComponent(sSubMatch2) & """" & sSubMatch3 & ">"
End Function
Function EncodeUriComponent(sText)
With CreateObject("htmlfile")
.ParentWindow.ExecScript (";")
EncodeUriComponent = .ParentWindow.EncodeUriComponent(sText)
End With
End Function
这篇关于经典的asp/vbscript-使用正则表达式修改所有href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!