我正在尝试用模式ASA####@@替换正文中的文本,并将其转换为ASA####@@(超链接)。
如果正文中只有一个模式,则我的代码可以正常工作。
但是,如果正文中有多个模式,例如:
ASA3422df
ASA2389ds
ASA1265sa
则整个正文都会被替换为
ASAhuyi65
以下是我的代码。
Dim strID As String
Dim Body As String
Dim objMail As Outlook.MailItem
Dim temp As String
Dim RegExpReplace As String
Dim RegX As Object
strID = MyMail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
Body = objMail.HTMLBody
Body = Body + "Test"
objMail.HTMLBody = Body
Set RegX = CreateObject("VBScript.RegExp")
With RegX
.Pattern = "ASA[0-9][0-9][0-9][0-9][a-z][a-z]"
.Global = True
.IgnoreCase = Not MatchCase
End With
'RegExpReplace = RegX.Replace(Body, "http://www.code.com/" + RegX.Pattern + "/ABCD")
'if the replacement is longer than the search string, future .FirstIndexes will be off
Offset = 0
'Set matches = RegX.Execute(Body)
For Each m In RegX.Execute(Body)
RegExReplace = "<a href=""http://www.code.com/" & m.Value & """>" & m.Value & "</a>"
Next
Set RegX = Nothing
objMail.HTMLBody = RegExReplace
objMail.Save
Set objMail = Nothing
End Sub
Blah blah <a href='http://www.code.com/$1'>$1</a>
等等,也就是不是你所报告的结果。然而,Body = RegX.Replace(Body, "<a href='http://www.code.com/$&'>$&</a>")
正确地返回了Blah blah <a href='http://www.code.com/ASA3422df'>ASA3422df</a>
等等。 - Jean-François Corbett