get .text() of an element, excluding text nodes of decendants which don't match a selector
I want to get the .text() of an element, excluding text nodes of descendants which don't match a selector, e.g. ":not(h2, h2 *, h3, h3 *)"
e.g. from the following example tree I want to get the string, "aaacccdddeee"
<div>
aaa
<h3>
bbb
</h3>
ccc
<div>
ddd
</div>
eee
</div>
The following doesn't work,
$(':not(h2, h2 *, h3, h3 *)', context).text()
- because for each matched element, it returns all descendant text nodes, so it returns some text nodes multiple times - "aaabbbcccdddeeeddd"
$(context).text() only returns each descendant text node once, but it doesn't filter descendants with the selector, so it returns "aaabbbcccdddeee"
Is there an easy way to do this with jQuery?