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(" ", ""); if (keyword.Length > 0 && !keywords.Contains(keyword)) { keywords.Add(keyword); } } for (int i = 0; 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[^>]*>)(?!.*\1)/ig,"");