有人知道JSoup的另一种选择吗?
或者如何清理序列<p> </p>?
用于jQuery的HTML Clean插件适用于我,但我有兴趣在服务器端执行html代码清理,而不是在客户端.
或者,要做的replaceAll表达式是什么?:
String cleanS = dirtyS.replaceAll("<p> </p>", ""); //This doesnt work
Run Code Online (Sandbox Code Playgroud)
我发现脏HTML带有混合序列的空格#160,还有其他像#32.
所以,我需要的是表达式去除它们的任何混合物.

你可以改变OutputSettings这个:
例:
final String html = ...;
OutputSettings settings = new OutputSettings();
settings.escapeMode(Entities.EscapeMode.xhtml);
String cleanHtml = Jsoup.clean(html, "", Whitelist.relaxed(), settings);
Run Code Online (Sandbox Code Playgroud)
这也可以Document通过Jsoup解析:
Document doc = Jsoup.parse(...);
doc.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
// ...
Run Code Online (Sandbox Code Playgroud)
编辑:
删除标签:
doc.select("p:matchesOwn((?is) )").remove();
Run Code Online (Sandbox Code Playgroud)
请注意:以后(?is)有没有空,但焦炭#160(= NBSP).这将删除所有自己的文本只是a的p-Tags .如果要对所有其他标记执行此操作,可以替换p:with *:.