...">

Regex for replacing <p> tags with line breaks

I have the following HTML

<p>Some text <a title="link" href="http://link.com/" target="_blank">my link</a> more 
text <a title="link" href="http://link.com/" target="_blank">more link</a>.</p>
<p>Another paragraph.</p>
<p>[code:cf]</p>
<p>&lt;cfset ArrFruits = ["Orange", "Apple", "Peach", "Blueberry", </p>
<p>"Blackberry", "Strawberry", "Grape", "Mango", </p>
<p>"Clementine", "Cherry", "Plum", "Guava", </p>
<p>"Cranberry"]&gt;</p>
<p>[/code]</p>
<p>Another line</p>
<p><img src="http://image.jpg" alt="Array" />
</p>
<p>More text</p>
<p>[code:cf]</p>
<p>&lt;table border="1"&gt;</p>
<p> &lt;cfoutput&gt;</p>
<p> &lt;cfloop array="#GroupsOf(ArrFruits, 5)#" index="arrFruitsIX"&gt;</p>
<p>  &lt;tr&gt;</p>
<p> &lt;cfloop array="#arrFruitsIX#" index="arrFruit"&gt;</p>
<p>     &lt;td&gt;#arrFruit#&lt;/td&gt;</p>
<p> &lt;/cfloop&gt;</p>
<p>  &lt;/tr&gt;</p>
<p> &lt;/cfloop&gt;</p>
<p> &lt;/cfoutput&gt;</p>
<p>&lt;/table&gt;</p>
<p>[/code]</p>
<p>With an output that looks like:</p>
<p><img src="another_image.jpg" alt="" width="342" height="85" /></p>

What I'm trying to do is write a regular expression that will delete everything <p>or </p>, and whenever it finds it </p>, it will replace it with a line break.

So far, my template has looked like this:

/\<p\>(.*?)(<\/p>)/g

And I will replace the matches:

$1\n

Everything looks good, but it also replaces the content inside the tags [code][/code], which in this case should not replace the tags at all <p>, so as a result I could get rid of <p>it when the content is not inside the tags [code].

I can never get denial, I know that it will be something like

\<p\>^\[code*\](.*?)(<\/p>)

But obviously this does not work :-)

Can someone help me with this regex?

, , HTML. , , , .

+3
3

, , HTML, , . ( , <p> </p> .)

, , ( look-behind look-ahead , <p>...</p> [code]...[/code], , IRC - JDK1.6.)

, code code, ( code ), <p>...</p> -, ( code .)

, ( ) code.

+1

, HTML . , , , .

?

+1

The syntax for the negative look is (?!).

(?![code.*?]([^\[]|\[\/[^c]|\[\/c[^o]|\[\/co[^d]|\[\/cod[^e]|\[\/code[^\]])*)<p>.*?</p>

0
source

Source: https://habr.com/ru/post/1741603/


All Articles