VBscript正则表达式替换
问题描述:
我不知道为什么这只适用于找到的最后一个实例,不是我所期望的所有实例。任何帮助赞赏。VBscript正则表达式替换
输入字符串:
<a href="http://www.scirra.com" target="_blank" rel="nofollow">http://www.scirra.com</a><br /><br />
<a href="http://www.scirra.com" target="_blank" rel="nofollow">http://www.scirra.com</a><br /><hr>
正则表达式:
'SEO scirra links
Dim regEx
Set regEx = New RegExp
' BB code urls
With regEx
.Pattern = "<a href=\""http://www.scirra.com([^\]]+)\"" target=\""_blank\"" rel=\""nofollow\"">"
.IgnoreCase = True
.Global = True
.MultiLine = True
End With
strMessage = regEx.Replace(strMessage, "<a href=""http://www.scirra.com$1"" target=""_blank"" title=""Some value insert here"">")
set regEx = nothing
输出:
<a href="http://www.scirra.com" target="_blank" rel="nofollow">http://www.scirra.com</a><br /><br />
<a href="http://www.scirra.com" target="_blank" title="Some value insert here">http://www.scirra.com</a><br /><hr>
谁能阐明了为什么只是将标题添加到上次找到的实例? (我有更多的测试,总是只适用于最后一个)
答
正是因为这个在你的正则表达式:
...a.com-->([^\]]+)<--
你尝试和匹配的一切是不是]
,一次或多次,在你的输入。并且由于在输入中根本没有]
,它会吞下所有内容(是,甚至是换行符),但必须回溯才能满足其余的正则表达式,这意味着它回溯到发生" target="_blank" ....
的最后。
如果你想更换rel="nofollow"
,让后面http://www.scirra.com
任何路径,你可以用这个表达式来代替:
(<a href="http://www\.scirra\.com((/[^/"]+)*/?)" target="_blank")rel="nofollow">
,并替换成:
$1title="Some value insert here">
复制/粘贴您当前的代码:
Dim regEx
Set regEx = New RegExp
' BB code urls
With regEx
.Pattern = "(<a href=""http://www\.scirra\.com((/[^""/]+)*/?)"" target=\""_blank\"")rel=\""nofollow\"">"
.IgnoreCase = True
.Global = True
.MultiLine = True
End With
strMessage = regEx.Replace(strMessage, "$1title=""Some value insert here"">")
但请注意,这是曲ite限制在被替换的URL中。例如,是否有目标内容可能是别的东西,还是有更多的属性?
谢谢!它的意思是匹配所有以http://www.scirra.com开头的网址,剥离nofollow。我仍然努力工作,'http://www.scirra.com(。*)'不符合他们任何一个,我需要什么? – 2012-01-14 02:10:15
“剥离nofollow”?你什么意思? – fge 2012-01-14 02:10:52
这是我正在对论坛进行的一项修改,我正在剥离网站内部发布的链接的nofollow属性以及添加标题属性 – 2012-01-14 02:12:36