0

I want to check some string containing HTML to see if it will contain any <li> elements, and filter out every element except for the <li> elements.

For example,

<li>foo</li><p><em>bar</em><em style="color: rgb(161, 0, 0);">fiddle</em></p><p><em class="ql-font-couriernew" style="color: rgb(61, 20, 102);">dog</em></p>

Would become

<li>foo</li>barfiddledog

I can figure out how to capture just <li> elements, or all HTML elements, but I can't figure out how to capture only elements that are not <li>.

I am using (<\/?[^(li)]>)+, but this seems to only filter out the <p> and </p> tags.

Acorn
  • 949
  • 5
  • 22

1 Answers1

1

Something like <\/?(?:(?!li).)+> should do the trick. I've taken notes from https://stackoverflow.com/a/977294/9484862 to edit your existing regex code, which was already nearly working.

Harrison Smith
  • 404
  • 3
  • 9
  • Awesome! I had taken a look at that before posting, but couldn't quite get it working. Regex is a mystery half the time. What you have worked most of the way, but it was filtering out the closing `` tag too. Not too big of a deal, but I was also able to extend it to not filter out the closing `` tag as well with `()` https://regex101.com/r/Sm4Azv/2 And thank you so much for actually answering instead of masturbating about dom objects for pixel points – Acorn May 12 '20 at 22:18