C# 檢查字串中是否有HTML標籤、返回過濾掉所有的HTML標籤後的字串
阿新 • • 發佈:2021-11-11
/// <summary> /// 檢查字串中是否有Html標籤 /// </summary> /// <param name="html">Html原始碼</param> /// <returns>存在為True</returns> public static bool CheckHtml(string html) { string filter = "<[\\s\\S]*?>"; if(Regex.IsMatch(html, filter)) { return true; } filter = "[<>][\\s\\S]*?"; if (Regex.IsMatch(html, filter)) { return true; } return false; }
/// <summary>/// 返回過濾掉所有的Html標籤後的字串 /// </summary> /// <param name="html">Html原始碼</param> /// <returns>過濾Html標籤後的字串</returns> public static string ClearAllHtml(string html) { string filter = "<[\\s\\S]*?>"; if (Regex.IsMatch(html, filter)) { html= Regex.Replace(html, filter, ""); } filter = "[<>][\\s\\S]*?"; if (Regex.IsMatch(html, filter)) { html = Regex.Replace(html, filter, ""); } return html; }