62,046
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 去掉html标记
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
protected static string ConvertGettext(string str)
{
Regex regex = new Regex(@"<(\w+)>[ \r\n\s\t]*?</\1>", RegexOptions.IgnoreCase);
return regex.Replace(str, "").Replace(" ", "").Replace("\n", "").Replace("\r", "");
}
string str = "<div> <p> </p>、<p> </p>、<p></p></div><div><span> <a> </a> </span></div><div>11111111</div>";
Regex reg = new Regex(@"<(?<bq>[^>]+)>( |\s)*</\k'bq'>");
MatchCollection mc = reg.Matches(str);
while (mc.Count > 0)
{
foreach (Match m in mc)
{
str = str.Replace(m.Value, "");
}
mc = reg.Matches(str);
}
/// <summary>
/// 去掉html标记
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
protected static string ConvertGettext(string str)
{
Regex regex = new Regex(@"<[ \r\n\s\t]*?>", RegexOptions.IgnoreCase);
return regex.Replace(str, "").Replace(" ", "").Replace("\n", "").Replace("\r", "");
}
protected string ConvertGettext(string str)
{
Regex regex = new Regex(@"<[^/>]+>( |\ \;)*</[^>]+>");
while (true)//循环替换,防止元素嵌套
{
str = regex.Replace(str, "");
if (!regex.IsMatch(str))
break;
}
return str;
}
ConvertGettext("<div> <p> </p>、<p> </p>、<p></p></div><div><span> <a> </a> </span></div><div>11111111</div>");
/// <summary>
/// 去掉html标记
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
protected static string ConvertGettext(string str)
{
Regex regex = new Regex(@"\<(.*?)\>", RegexOptions.IgnoreCase);
return regex.Replace(str, "").Replace(" ", "").Replace("\n", "").Replace("\r", "");
}