How to select text in an HTML tag without a tag around it (JSoup)

I would like to select the text inside a strong tag, but without the div below it ...

Is there any way to do this with jsoup directly?

My attempt at selection (doesn't work, selects the full content inside a strong tag):

Elements selection = htmlDocument.select("strong").select("*:not(.dontwantthatclass)");

HTML:

<strong>
   I want that text
   <div class="dontwantthatclass">
   </div>
</strong>
+4
source share
3 answers

You are looking for the ownText () method .

String txt = htmlDocument.select("strong").first().ownText();
+6
source

, jsoup https://jsoup.org/apidocs/org/jsoup/nodes/Element.html. remove(), removeChild() .. , , . , start end, </br> https://www.debuggex.com/r/1gmcSdz9s3MSimVQ

,

selection.replace(/<([^ >]+)[^>]*>.*?<\/\1>|<[^\/]+\/>/ig, "");

, .

, , - javascript vbscript: -

Elements selection = htmlDocument.select("strong")

jquery: -

var removeHTML = function(text, selector) {
    var wrapped = $("<div>" + text + "</div>");
    wrapped.find(selector).remove();
    return wrapped.html();
}

ownText() jsoup .

+1

, jQuery, innerText "" :

var selection = htmlDocument.select("strong")[0].innerText;

https://jsfiddle.net/scratch_cf/8ds4uwLL/

PS: If you want to wrap the resulting text in a "strong" tag, I think you will need to create a new element, for example $('<strong>retrievedText</strong>');

0
source

All Articles