将内容转换为 XML,然后简单地搜索a
包含href
以mailto:
你需要这个来使用 XPath:using System.Xml.XPath;
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.";
XElement x = XElement.Parse(string.Format("<root>{0}</root>", content));
var hrefs = x.XPathSelectElements("a[starts-with(@href, 'mailto:')]");
foreach (XElement href in hrefs)
{
href.Attribute("href").Value = "#";
href.Add(new XAttribute("title", "protected"));
string email = href.Value;
int at = email.IndexOf('@');
if(at > 0)
{
string username = email.Substring(0, at);
string domain = email.Substring(at);
if (username.Length > 2)
href.Value = string.Format("{0}..{1}",
username.Substring(0, 2), domain);
}
}
string result = string.Concat(x.Nodes().ToArray());