mythereal
mythereal

Reputation: 803

Match grouped list elements with regex

I'm sure this question has been asked before, but I'm unable to find it.

I have a bunch of list elements like so:

<li class="list-item">aewfaffjpiowfj: apeifjeawpfioj</li>
<li class="list-item">aewfaffjpiowfj: apeifjeawpfioj</li>
<li class="list-item">aewfaffjpiowfj: apeifjeawpfioj</li>
<li class="list-item">aewfaffjpiowfj: apeifjeawpfioj</li>
<br><br>

sample text
<li class="list-item">aewfaffjpiowfj: apeifjeawpfioj</li>
<li class="list-item">aewfaffjpiowfj: apeifjeawpfioj</li>

more text
<li class="list-item">aewfaffjpiowfj: apeifjeawpfioj</li>

I want to wrap each group in a <ul></ul> element. How do I do that?

This is what I've tried, but it only matches the individual elements: /<li [^>]*>.*<\/li>/g

https://regex101.com/r/KXEkJz/1

And if I do /<li [^>]*>[\s\S]*<\/li>/g, it produces one giant match.

Upvotes: 2

Views: 566

Answers (1)

snollygolly
snollygolly

Reputation: 1886

If you want to group them by new lines, try something like this:

(<li [^>]*>.*<\/li>\n?)+

The only difference between it and yours is that it tries to capture multiple <li> objects and doesn't mind if there is a new line. If there's a new line followed by something different though, it stops matching.

Upvotes: 3

Related Questions