I am using JSoup parser to find particular parts of a html document (defined by regex) and highlight it by wrapping the found string in <span> tag. Here is my code that does the highlighting –
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();
}
It does work but the tag <span> is visible inside the webview. What am I doing wrong?
Looks like no one knows. Here’s some code that i’ve come up with. Slow and inefficient but works anyway. Suggestions are accepted 🙂
This class can be used to highlight any html using a regex.
you have to call these two methods to get the highlighted html –
This will highlight everything that matches the regex
(abc)|(def)*.You can change the way you want the regex to be built by
modifying buildRegexFromQuery()function.