Asp.Net正则获取页面a标签里的内容

时间:2022-05-09 08:34:16

Asp.Net正则获取页面a标签里的内容

    string url = "http://www.114369.com";
string html = MyCLib.NetClass.SendUrl(url,System.Text.Encoding.UTF8);
List<string> keywords = new List<string>();
Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");
MatchCollection mc = reg.Matches(html);
foreach (Match m in mc)
{
//richTextBox2.Text += m.Groups["url"].Value + "\n";
string keyword = Regex.Replace(m.Groups["text"].Value, "<[^>]*>", string.Empty).Replace("..", "").Replace("·", "").Replace("&nbsp;", ""); if (keyword.Length > && !keywords.Contains(keyword))
{
keywords.Add(keyword);
}
}
for (int i = ; i < keywords.Count; i++)
{
Response.Write(keywords[i]);
Response.Write("<br>");
}

 Asp.Net正则过滤超链接a

string s = "<a href=\"#\">我们是中国人</a><a class='xxx' href=\"#\">我们是中国人2</a>";
Regex reg = new Regex(@"<a\s*[^>]*>([\s\S]+?)</a>", RegexOptions.IgnoreCase);
s = reg.Replace(s, "$1");
Response.Write(s);//结果:我们是中国人我们是中国人2

js正则过滤超链接a

string s = "<a href=\"#\">我们是中国人</a><a class='xxx' href=\"#\">我们是中国人2</a>";

s = s.replace(/(<\/?a[^>]*>)(?!.*\)/ig,"");