Search code examples
regexregex-lookarounds

RegEx - match specific html tags between other tag


I´ve got a problem that I have to remove all HTML br tags in an unordered list. The input HTML looks like this

<br /><ul><br /><li>one</li><br /><li>two</li><br /></ul><br />

Now I have to remove the br tags in the unordered list (and not the ones before and following)

I tried to solve the problem via regular expressions

(?<=(\<ul.*\>))(.*)(?=(<\/ul>))

That gives me the text between the ul tags.

But how can I get only the br tags in the selection?

https://regexr.com/5jo2s

Thank you very much in advance!


Solution

  • The solution from Wiktor Stribizew (see above) is working

    /(?<=<ul[^>]*>.*?)<br \/>(?=.*?<\/ul>)/g