输入字符串:<b>Test link</b> <a href="https://www.w3schools.com">Visit W3Schools</a>
预期结果:Test link <a href="https://www.w3schools.com">Visit W3Schools</a>
我对jsoup的尝试:
public String cleanHtml(String html)
{
Whitelist whitelist = Whitelist.none();
whitelist.addTags("a");
return Jsoup.clean(html, whitelist);
}
结果是:
Test link <a>Visit W3Schools</a>
如何删除所有标签,但保留整个
a href
? 最佳答案
您需要使用addAttributes
。在这里,您传递允许的属性列表whitelist.addAttributes("a","href","id","more")
;
试试这个:
String html = "<b>Test link</b> <a href=\"https://www.w3schools.com\">Visit W3Schools</a>";
Whitelist whitelist = Whitelist.none();
whitelist.addTags("a");
whitelist.addAttributes("a","href");
System.out.println(Jsoup.clean(html, whitelist));