Javascript - replace tags in CMS formated content Pt. II
<p>I'm dealing with a CMS that formats a block of text like so:</p>

<blockquote>
  <p><code>&lt;p id="V01-cntntTxt"&gt;</code>Lorem ipsum dolor sit amet, consectetur adipiscing
  elit.<code>&lt;br&gt;</code>Vivamus lacus ipsum, semper non consequat eu, facilisis at
  lectus. Vestibulum et magna ac<code>&lt;br&gt;</code>odio semper porttitor lacinia
  congue orci. Vivamus suscipit eleifend dolor, in hendrerit<code>&lt;br&gt;</code>turpis
  bibendum ut.<code>&lt;br&gt;&lt;br&gt;</code>Morbi interdum augue et nisl ullamcorper sit
  amet ornare<code>&lt;br&gt;</code> lorem tempus. Duis nec nisi quis ipsum pulvinar
  volutpat. Suspendisse venenatis malesuada metus,<br>nec pretium dui
  cursus eget. Donec vitae lorem vitae risus dapibus malesuada congue
  vel nunc.<code>&lt;/p&gt;</code></p>
</blockquote>

<p>The company that uses this CMS has made it standard practice to use SHIFT+Enter x 2, to indicate a paragraph break, and a single Shift+Enter as a line break. I am not able to patch or change how the CMS works.</p>

<p>This is the desired result:</p>

<blockquote>
  <p><code>&lt;p id="V01-cntntTxt"&gt;</code>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus
  lacus ipsum, semper non consequat eu, facilisis at lectus. Vestibulum
  et magna ac odio semper porttitor lacinia congue orci. Vivamus
  suscipit eleifend dolor, in hendrerit turpis bibendum ut.<code>&lt;/p&gt;&lt;p&gt;</code>Morbi
  interdum augue et nisl ullamcorper sit amet ornare  lorem tempus. Duis
  nec nisi quis ipsum pulvinar volutpat. Suspendisse venenatis malesuada
  metus, nec pretium dui cursus eget. Donec vitae lorem vitae risus
  dapibus malesuada congue vel nunc.<code>&lt;/p&gt;</code></p>
</blockquote>

<p>I need to replace double <code>&lt;br&gt;&lt;br&gt;</code> tags with a closing and opening <code>&lt;/p&gt;&lt;p&gt;</code> tag to maintain the paragraph break, and then remove any remaining single <code>&lt;br&gt;</code> tags that are for line breaks.</p>

<p>I have tried this, and it almost works:</p>

<pre><code>&lt;script&gt;
$('#V01-cntntTxt').html($('#V01-cntntTxt').html().replace(/&lt;br&gt;&lt;br&gt;/g, '&lt;/p&gt;'));
$('#V01-cntntTxt').html($('#V01-cntntTxt').html().replace(/&lt;br&gt;/g, ' '));
&lt;/script&gt;
</code></pre>

<p>However, the result ends up with an empty paragraph. The two <code>&lt;/p&gt;&lt;/p&gt;</code> tags in the middle are backwards. It should be <code>&lt;/p&gt;&lt;p&gt;</code> and not <code>&lt;p&gt;&lt;/p&gt;</code>. It seems as though an opening tag is being inserted even though I have not specified that it should.</p>

<p>This is the result of the above script, with the tags backwards:</p>

<blockquote>
  <p><code>&lt;p id="V01-cntntTxt"&gt;</code>Lorem ipsum dolor sit amet, consectetur
  adipiscing elit. Vivamus lacus ipsum, semper non consequat eu,
  facilisis at lectus. Vestibulum et magna ac odio semper porttitor
  lacinia congue orci. Vivamus suscipit eleifend dolor, in hendrerit
  turpis bibendum ut.<code>&lt;p&gt;&lt;/p&gt;</code>Morbi interdum augue et nisl ullamcorper
  sit amet ornare lorem tempus. Duis nec nisi quis ipsum pulvinar
  volutpat. Suspendisse venenatis malesuada metus, nec pretium dui
  cursus eget. Donec vitae lorem vitae risus dapibus malesuada congue
  vel nunc.<code>&lt;/p&gt;</code></p>
</blockquote>
