Refer to nested text pattern not working? Problem ::after tag

Refer to nested text pattern not working? Problem ::after tag

Assume I have (simplified) the following code:

<div ...>
other text to keep
<div  ...>
<p>
<em>
<a ....>
.....foobar....
</a>
</em>
</p>
::after
</div>
</div>

How do I remove all (ONLY !) innermost <div> which contain the pattern "foobar"?

Since ::after comes after the target elements it should not be relevant.
correct?

$("div:contains('foobar')").eq(($("div:contains('foobar')").length) - 1).css("display", "none");

does not work