用嵌入的超链接替换文本 [英] Replace text with embedded hyperlinks

查看:114
本文介绍了用嵌入的超链接替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本正文中搜索ASA1234yy并将其替换为超链接ASA1234yy

您能帮我开始吗?我尝试使用替换功能,但它仅替换文本.我希望将文本替换为超链接.

到目前为止的代码已完成

I want to search for ASA1234yy in the body of a text and replace it with the hyperlink ASA1234yy

Can you help me to get started. I have tried using Replace function, but it Replaces only text. I want the text to be replaced with a hyperlink.

Code done so far

Sub ConvertToHyperlink(MyMail As MailItem)
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.Body
Body = Body + "Test"
objMail.Body = 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")

Set RegX = Nothing
objMail.Body = RegExpReplace
objMail.Save
Set objMail = Nothing
End Sub



我需要这样的输出

例如:如果文本正文中有一些ASA1234yy,则应将其替换为ASA1234yy中特定的嵌入式超链接.该超链接的地址应为http://www.code.com/ASA1234yy/ABCD

请帮助我.



I need to have a output like this

For example :If there is some ASA1234yy in the body of a text we should replace it with their specific embedded hyperlink in ASA1234yy.The hyperlink should have the address http://www.code.com/ASA1234yy/ABCD

Please help me.

推荐答案


考虑到您要用"http://www.code.com/ASA1234yy/ABCD"之类的链接替换"ASA1234yy"之类的文本,请执行以下操作,

Hi
Considering that you want to replace text like "ASA1234yy" with a link like "http://www.code.com/ASA1234yy/ABCD", please do the following ,

Set RegX = CreateObject("VBScript.RegExp")
sBody ="gshaai sdijfASA1234yy dds"
msgbox isObject(RegX)
With RegX
.Pattern = "ASA\d{4}\w{2}"
.Global = True
.IgnoreCase = Not MatchCase
End With
RegExpReplace = RegX.Replace(sBody, "http://www.code.com/ASA1234yy/ABCD")
msgbox RegExpReplace 



您的代码还可以,我唯一改变的就是正则表达式模式.

现在,如果要替换http网址中的每个捕获值,则需要先使用模式获取所有匹配项,然后必须更新url,帮助代码在此处,



Your code was Ok only thing I changed is the regex pattern.

Now, in case you want to replace each captured value inside your http url, the you need to get all matches using the pattern first then you have to update the url,helper code is here,

Set RegX = CreateObject("VBScript.RegExp")
sBody = "This is ASA4567sa a sampleASA3333df text to show ASA5555sastring replacement in betASA9999ttween"
msgbox isObject(RegX)
With RegX
.Pattern = "ASA\d{4}\w{2}"
.Global = True
.IgnoreCase = Not MatchCase
End With
Set matches = RegX.Execute(sBody)
msgbox matches.Count
If matches.Count > 0 Then    
    For Each match In matches
       RegExpReplace = RegX.Replace(sBody, "http://www.code.com/"+match.value+"/ABCD")      
    Next
Else
    msgbox "No match", 0, "ADT"
End If
msgbox RegExpReplace 



上面的代码:
输入字符串:这是ASA4567a,它是一个示例ASA3333df文本,用于显示betASA9999ttween中的ASA5555sastring替换".
输出字符串:这是http://www.code.com/ASA9999tt/ABCD示例http://www.code.com/ASA9999tt/ABCD文本,用于显示http://www.code .com/ASA9999tt/ABCD替换字符串http://www.code.com/ASA9999tt/ABCDween"
[替换4场比赛]

让我知道是否有帮助.



Above code:
Input string : "This is ASA4567sa a sampleASA3333df text to show ASA5555sastring replacement in betASA9999ttween"
Output String : "This is http://www.code.com/ASA9999tt/ABCD a samplehttp://www.code.com/ASA9999tt/ABCD text to show http://www.code.com/ASA9999tt/ABCDstring replacement in bethttp://www.code.com/ASA9999tt/ABCDween"
[Replaced 4 matches]

Let me know if that helps.




试试

Hi,

Try

RegExpReplace = RegX.Replace(sBody, "<a href=''http://www.code.com/''"+match.value+"''/ABCD''>match.value</a>")


但是请确保您的邮件类型不是简单的文本",它必须是html型邮件.

让我知道是否有帮助:)


But please make sure that your mail message is of type other that simple "text", it need to be html type mail message.

Let me know if that helps :)


这篇关于用嵌入的超链接替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆