/// <summary> /// 清除文本中的HTML标记(保存换行符) /// </summary> /// <param name="str">待处理的字符串</param> /// <returns>string</returns> public static string ClearHtml2(this string HTML) { if (String.IsNullOrWhiteSpace(HTML)) { return(String.Empty); } string[] Regexs = { @"</?([a-z]+)([^<]*)>", @"&(quot|#34);", @"&(amp|#38);", @"&(lt|#60);", @"&(gt|#62);", @"&(nbsp|#160);", @"&(iexcl|#161);", @"&(cent|#162);", @"&(pound|#163);", @"&(copy|#169);", @"&#(\d+);", @"-->", @"<!--.*\n", @"\[(\d+)?\]", @"\[E\:(\d+)?\|(\d+)?\|(\d+)?\]" }; string[] Replaces = { String.Empty, "\"", "&", "<", ">", " ", "\xa1", //chr(161), "\xa2", //chr(162), "\xa3", //chr(163), "\xa9", //chr(169), String.Empty, "\r\n", String.Empty, string.Empty, string.Empty }; string s = HTML; for (int i = 0, len = Regexs.Length; i < len; i++) { s = RegExt.CreateFromCache(Regexs[i], RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(s, Replaces[i]); } s.Replace("<", String.Empty); s.Replace(">", String.Empty); return(s); }
/// <summary> /// 清除空格 /// </summary> /// <param name="s"></param> /// <returns></returns> public static string TrimEmpty(this string s) { if (!string.IsNullOrWhiteSpace(s)) { var reg = RegExt.CreateFromCache("\\s+", System.Text.RegularExpressions.RegexOptions.IgnoreCase); return(s = reg.Replace(s, string.Empty)); } return(s); }
/// <summary> /// 是否是字母 /// </summary> /// <param name="p"></param> /// <returns></returns> public static bool IsLetter(this string content) { Regex regex = RegExt.CreateFromCache(@"[^A-Za-z]+"); if (regex.IsMatch(content)) { return(false); } return(true); }
/// <summary> /// 清除HTML标签中的Style属性 /// </summary> /// <param name="HTML"></param> /// <returns></returns> public static string ClearHtmlStyle(this string HTML) { if (string.IsNullOrWhiteSpace(HTML)) { return(string.Empty); } var re = RegExt.CreateFromCache("style\\=\".+?\"|style\\='.+?'", RegexOptions.IgnoreCase | RegexOptions.Multiline).Replace(HTML, string.Empty); return(re); }
public static string ClearHtml3(this string HTML) { if (string.IsNullOrWhiteSpace(HTML)) { return(string.Empty); } var reg = RegExt.CreateFromCache(@"(?:\>)\w+(?:\<)", RegexOptions.Multiline | RegexOptions.IgnoreCase); var matches = reg.Matches(HTML); if (matches != null && matches.GetEnumerator().MoveNext()) { var s = matches.OfType <Match>().ToArray().Select(i => i.Value); HTML = string.Join("", s); } HTML = HTML.Replace("<", string.Empty).Replace(">", string.Empty); return(HTML); }
/// <summary> /// 清除文本中的HTML标记 /// </summary> /// <param name="str">待处理的字符串</param> /// <returns>string</returns> public static string ClearHtml(this string HTML) { if (String.IsNullOrWhiteSpace(HTML)) { return(String.Empty); } var regexdicts = new Dictionary <string, string>(); regexdicts.Add(@"</?([a-z]+)([^<]*)>", String.Empty); regexdicts.Add(@"([\r\n])[\s]+", String.Empty); regexdicts.Add(@"&(quot|#34);", "\""); regexdicts.Add(@"&(amp|#38);", "&"); regexdicts.Add(@"&(lt|#60);", "<"); regexdicts.Add(@"&(gt|#62);", ">"); regexdicts.Add(@"&(nbsp|#160);", " "); regexdicts.Add(@"&(iexcl|#161);", "\xa1"); regexdicts.Add(@"&(cent|#162);", "\xa2"); regexdicts.Add(@"&(pound|#163);", "\xa3"); regexdicts.Add(@"&(copy|#169);", "\xa9"); regexdicts.Add(@"&#(\d+);", String.Empty); regexdicts.Add(@"<!--.*-->", String.Empty); regexdicts.Add("<img[^>]*>", String.Empty); string s = HTML; foreach (var key in regexdicts.Keys) { s = RegExt.CreateFromCache(key, RegexOptions.IgnoreCase | RegexOptions.Multiline).Replace(s, regexdicts[key]); } s.Replace("<", String.Empty); s.Replace(">", String.Empty); s.Replace("\r\n", String.Empty); return(s); }