如何从字符串通过正则表达式删除HTML标签? [英] How can i remove HTML Tags from String by REGEX?

查看:427
本文介绍了如何从字符串通过正则表达式删除HTML标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从MySQL中获取数据,但问题是HTML标签即

 &放大器; LT; P&放大器; GT;大&放; LT; / p&放大器; GT;&放大器; LT; p&放大器; GT;羔羊;< BR> ;;立; UL;&安培; NBSP; 

也被获取我的数据我只需要大,并从上面的行羔羊。我怎么能分开字符串/删除HTML标签?


< DIV CLASS =h2_lin>解决方案

试试这个




  //删除字符串中的
html标签公共静态字符串StripHtml(目标字符串)
{
// HTML标记
正则表达式StripHTMLExpression正则表达式=新正则表达式(< \\S [^><] *>中,RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled);

返回StripHTMLExpression.Replace(目标的String.Empty);
}




呼叫




 字符串htmlString =< D​​IV><跨度>世界你好<!/ SPAN>< / DIV>中; 
串strippedString = StripHtml(htmlString);


I am fetching data from Mysql but the issue is "HTML tags i.e.

&lt;p&gt;LARGE&lt;/p&gt;&lt;p&gt;Lamb;<br>;li;ul;&nbsp;

also being fetched with my data i just need "LARGE" and "Lamb" from above line. How can I separate/remove HTML tags from String?

解决方案

try this

// erase html tags from a string
public static string StripHtml(string target)
{
//Regular expression for html tags
Regex StripHTMLExpression = new Regex("<\\S[^><]*>", RegexOptions.IgnoreCase |   RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled);

return StripHTMLExpression.Replace(target, string.Empty);
}

call

string htmlString="<div><span>hello world!</span></div>";
string strippedString=StripHtml(htmlString);

这篇关于如何从字符串通过正则表达式删除HTML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆