Fetch previous and next element with a certain condition

Fetch previous and next element with a certain condition

Lets start with a sample source:

  1. <hr>
  2. <p>....foobar...</p>
  3. <hr>

Now I want to delete all <hr> elements which are immediately

a) BEFORE a <p> element which contain "foobar"
b) AFTER a <p> element which contain "foobar"

If I code for example

$("p:contains('foobar')").prev().remove();

then elements might be delete which are NOT a <hr> element

So how can I specify otherwise the delete statement of  a sequence of elements with one condition?

Peter