从字符串中移除HTML标签 [英] Remove HTML tags from a String
问题描述
有没有一种从Java字符串中移除HTML的好方法?一个简单的正则表达式像
replaceAll(\\<。*?>,)
可以工作,但是像& amp;
wont被正确地转换,两个尖括号之间的非HTML将被删除(即正则表达式中的。*?
将消失)。
使用HTML解析器而不是正则表达式。使用 Jsoup ,这很简单。
public static String html2text(String html){
return Jsoup.parse(html).text();
}
Jsoup也支持根据可定制的白名单移除HTML标记,如果您只允许例如 Is there a good way to remove HTML from a Java string? A simple regex like will work, but things like Use a HTML parser instead of regex. This is dead simple with Jsoup. Jsoup also supports removing HTML tags against a customizable whitelist, which is very useful if you want to allow only e.g. 这篇关于从字符串中移除HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!< b>
,< i>
和< u> $ c
$ b $ h3另请参见:
$ ul $ b $ li
replaceAll("\\<.*?>","")
&
wont be converted correctly and non-HTML between the two angle brackets will be removed (i.e. the .*?
in the regex will disappear). public static String html2text(String html) {
return Jsoup.parse(html).text();
}
<b>
, <i>
and <u>
.See also: