JSFiddle - React, Tailwind, and code Playground
HTML
<div id="1">Hello
<div id="2">World</div> <div>div1<div> div2 </div></div>
end
</div>
JavaScript
var words=new Array()
getwords($("body"))
function getwords(e){
e.contents().each(function(){
if ( $(this).children().length > 0 ) {
getwords($(this))
}
else if($.trim($(this).text())!=""){
words=words.concat($.trim($(this).text()).split(/\W+/))
}
});
}
console.log(words)