I'm working on a number of HTML files and I'm trying to match a <p>
tag inside a <li>
inside a <ul>
For example:
<ul>
<li>1</li>
<li><p>2</p></li>
<li>
<ul>
<li><p>3</p></li>
</ul>
</li>
</ul>
My goal is to match both <p>
tags (2 and 3) separately with their nearest parent <li>
and <ul>
tags.
Here's the Regex I'm using
/<ul>.*?(<li.*?>).*?(<p.*?>.*?<\/p>)(.*?)(<\/li>)/gs
Problem happens when I try to match in an html like this:
<ul>
<li>
<ul>
<li></li>
<p>4</p>
</ul>
</li>
</ul>
It matches the <p>
tag and the further away parent <li>
and <ul>
tags.
Does anyone have an idea how I can fix this?
Edit: Assuming I need to use Regex for this matching. I might end up using selectors in JS anyway like you guys suggested, but I'd still like to know if there's an easy fix for this pattern since this logic already exists in my app using Regex.