替换邮件链接 [英] Replace mailto-links

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

问题描述

我有以下示例字符串:

var content = "Lorem ipsum dolor sit amet <a href=\"mailto:info@xxx.com\">info@xxx.com</a> ipsum dolor <a href=\"mailto:info@yyy.eu\">info@yyy.eu</a> adipiscing elit.";

当字符串包含mailto-link时,我想替换(操纵)所有的a-tag.

I want to replace (manipulate) all occurrences of the of the a-tag when the string contains a mailto-link.

操纵后的锚应该看起来像这样:

The anchor's should look like after manipulation:

<a href="#" title="protected">in..@xxx.com</a>

内容字符串可以包含任意数量的邮件链接.

The content string could contain any number of mailto-links.

如何最好地执行此任务?

How can I best perform this task?

推荐答案

如果您有XHTML,请使用Chuck显示的XElement.

If you have XHTML then use XElement as Chuck showed.

如果没有,那么使用正则表达式是可行的方法.像这样:

If not, then regular expressions are the way to go. Something like:

Regex find = new Regex("<a\\b[^>]*href=['\"]mailto:(.*?)['\"]", RegexOptions.Singleline | RegexOptions.IgnoreCase);

警告,我没有测试上面的代码,但我99%确信它是正确的.另外,我可能错过了电子邮件地址中的大写字母,例如>.

Warning, I did not test the above code but I'm 99% certain it is correct. Also, I may have missed a corner case such as a > in the email address.

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

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