I’m trying to match a specific element with regex that is followed by a particular closing tag, but am having trouble where the text contains more than one of those elements. So in this code I want to match the second whole strong element because it’s followed by a closing p
<strong>aaa</strong>kjrfhlkjrnrjnv<strong>bbb</strong></p>
I tried this regex <strong>.+?</strong>(?=</p>)
but it matches everything up to the </p>
. Without the lookahead, both of the strongs match separately. So it seems the lookahead makes regex carry on after the first </strong>
because it didn’t match, until it matches on the second. I’m wondering if there is a way to get it to only match the second strong element? This is a simplified example, so they could be other elements with various content and attributes, and there could be any number of them. Basically I want to match a whole particular element followed by something and not any other preceding elements of the same type.
cabrito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1