/// <summary>
/// 清除外部链接 a href
/// </summary>
/// <param name="strHtml"></param>
/// <returns></returns>
public static string ClearExternalHrefByHtml(string strHtml)
{
//去掉外部链接
//string pattern = @"<a[^>]*>[^<]+</a>\s*";
string pattern = @"<a[^>]*>(.*?)</a>\s*";
const string instrument = "instrument.com.cn";
MatchCollection macths = Regex.Matches(strHtml, pattern, RegexOptions.IgnoreCase);
foreach (Match macth in macths)
{
string temptag = macth.ToString();
Match m = Regex.Match(temptag, @"<a[^>]*?href=""(?<url>[^""]*)""\s*[^>]*>(?<text>[\s\S]*?)</a>", RegexOptions.IgnoreCase);
if (m.Success)
{
string url = m.Groups["url"].Value.ToLower();
string text = m.Groups["text"].Value;
if (url.IndexOf(instrument) <= 0)
{
strHtml = strHtml.Replace(temptag, text);
}
}
}
return strHtml;
}
清除外部链接
最新推荐文章于 2022-02-15 16:08:49 发布