0

I need to wrap multiple groups of li tags with ul tags. Here is a sample:

<text>
<p>Today is a nice day</p>
<li>This is line one</li>
<li>This is line two</li>
<li>This is line three</li>
</text>
<text>
<p>Today is also a nice day</p>
<li>This is also line one</li>
<li>This is also line two</li>
<li>This is also line three</li>
</text>

Here is the regex that I am using:

/(<li>.*?<\/li>)+/g

When I try to replace the selected string with:

<ul>$1</ul> 

I get the following:

<text>
<p>Today is a nice day</p>
<ul><li>This is line one</li></ul>
<ul><li>This is line two</li></ul>
<ul><li>This is line three</li></ul>
</text>
<text>
<p>Today is also a nice day</p>
<ul><li>This is also line one</li></ul>
<ul><li>This is also line two</li></ul>
<ul><li>This is also line three</li></ul>
</text>

Here is what I want the result to look like:

<text>
<p>Today is a nice day</p>
<ul><li>This is line one</li></ul>
<ul><li>This is line two</li></ul>
<ul><li>This is line three</li></ul>
</text>
<text>
<p>Today is also a nice day</p>
<ul>
<li>This is also line one</li>
<li>This is also line two</li>
<li>This is also line three</li>
</ul>
</text>
Paul Maine
  • 51
  • 1
  • 10

1 Answers1

1

You have to match all the <il> tags in one single match, so you need to use some flavor of multiline mode: s modifier makes . match against newline:

/(</p><li>.*?</li></text>)+/is

Also check this question/answer

Community
  • 1
  • 1
Chosmos
  • 596
  • 9
  • 11