1
我使用JSoup分析器來查找html文檔的特定部分(由正則表達式定義),並通過包裝在<span>
標記中找到的字符串突出顯示它。這裏是我的代碼,不會高亮 -突出在JSOUP中使用正則表達式爲android
public String highlightRegex() {
Document doc = Jsoup.parse(htmlContent);
NodeTraversor nd = new NodeTraversor(new NodeVisitor() {
@Override
public void tail(Node node, int depth) {
if (node instanceof Element) {
Element elem = (Element) node;
StringBuffer obtainedText;
for(Element tn : elem.getElementsMatchingOwnText(pat)) {
Log.e("HELLO", tn.baseUri());
Log.e("HELLO", tn.text());
obtainedText = new StringBuffer(tn.ownText());
mat = pat.matcher(obtainedText.toString());
int nextStart = 0;
while(mat.find(nextStart)) {
obtainedText = obtainedText.replace(mat.start(), mat.end(), "<span>" + mat.group() + "</span>");
nextStart = mat.end() + 1;
}
tn.text(obtainedText.toString());
Log.e("HELLO" , "AFTER:" + tn.text());
}
}
}
@Override
public void head(Node node, int depth) {
}
});
nd.traverse(doc.body());
return doc.toString();
}
它的工作,但是標籤<span>
是web視圖內可見。我究竟做錯了什麼?