是否有一个好方法从Java字符串中删除HTML ?一个简单的正则表达式
replaceAll("\\<.*?>", "")
会起作用,但有些东西像&将不能正确地转换,并且两个尖括号之间的非html将被删除(即。*?在正则表达式中将消失)。
是否有一个好方法从Java字符串中删除HTML ?一个简单的正则表达式
replaceAll("\\<.*?>", "")
会起作用,但有些东西像&将不能正确地转换,并且两个尖括号之间的非html将被删除(即。*?在正则表达式中将消失)。
当前回答
这里是如何替换所有(HTML标签| HTML实体|空白的HTML内容)的另一个变体
content.replaceAll ("(<.*?>)|(&.*?;)|([ ]{ 2 ,})", "");其中content是一个字符串。
其他回答
有时html字符串来自xml,带有这样的<。在使用Jsoup时,我们需要解析它,然后清理它。
Document doc = Jsoup.parse(htmlstrl);
Whitelist wl = Whitelist.none();
String plain = Jsoup.clean(doc.text(), wl);
而仅使用Jsoup.parse(htmlstrl).text()不能删除标签。
我的5美分:
String[] temp = yourString.split("&");
String tmp = "";
if (temp.length > 1) {
for (int i = 0; i < temp.length; i++) {
tmp += temp[i] + "&";
}
yourString = tmp.substring(0, tmp.length() - 1);
}
这里是如何替换所有(HTML标签| HTML实体|空白的HTML内容)的另一个变体
content.replaceAll ("(<.*?>)|(&.*?;)|([ ]{ 2 ,})", "");其中content是一个字符串。
您可以使用此代码删除HTML标记,包括换行符。
function remove_html_tags(html) {
html = html.replace(/<div>/g, "").replace(/<\/div>/g, "<br>");
html = html.replace(/<br>/g, "$br$");
html = html.replace(/(?:\r\n|\r|\n)/g, '$br$');
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
html = tmp.textContent || tmp.innerText;
html = html.replace(/\$br\$/g, "\n");
return html;
}
接受的答案并不适用于我所指出的测试用例:“a < b or b > c”的结果是“a b or b > c”。
所以,我用TagSoup代替。下面是一个对我的测试用例(以及其他一些测试用例)有效的示例:
import java.io.IOException;
import java.io.StringReader;
import java.util.logging.Logger;
import org.ccil.cowan.tagsoup.Parser;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* Take HTML and give back the text part while dropping the HTML tags.
*
* There is some risk that using TagSoup means we'll permute non-HTML text.
* However, it seems to work the best so far in test cases.
*
* @author dan
* @see <a href="http://home.ccil.org/~cowan/XML/tagsoup/">TagSoup</a>
*/
public class Html2Text2 implements ContentHandler {
private StringBuffer sb;
public Html2Text2() {
}
public void parse(String str) throws IOException, SAXException {
XMLReader reader = new Parser();
reader.setContentHandler(this);
sb = new StringBuffer();
reader.parse(new InputSource(new StringReader(str)));
}
public String getText() {
return sb.toString();
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
for (int idx = 0; idx < length; idx++) {
sb.append(ch[idx+start]);
}
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
sb.append(ch);
}
// The methods below do not contribute to the text
@Override
public void endDocument() throws SAXException {
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
}
@Override
public void processingInstruction(String target, String data)
throws SAXException {
}
@Override
public void setDocumentLocator(Locator locator) {
}
@Override
public void skippedEntity(String name) throws SAXException {
}
@Override
public void startDocument() throws SAXException {
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
}
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
}
}