I'm dealing with a CMS that formats a block of text like so:
<p id="V01-cntntTxt">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
Vivamus lacus ipsum, semper non consequat eu, facilisis at lectus. Vestibulum et magna ac<br>
odio semper porttitor lacinia congue orci. Vivamus suscipit eleifend dolor, in hendrerit<br>
turpis bibendum ut.<br><br>
Morbi interdum augue et nisl ullamcorper sit amet ornare<br>
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.</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.
This is the desired result:
<p id="V01-cntntTxt">
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.</p><p>
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.</p>
I need to replace double <br><br>
tags with a closing and opening </p><p>
tag to maintain the paragraph break, and then remove any remaining single <br>
tags that are for line breaks.
I have tried this, and it almost works:
<script>
$('#V01-cntntTxt').html($('#V01-cntntTxt').html().replace(/<br><br>/g, '</p>'));
$('#V01-cntntTxt').html($('#V01-cntntTxt').html().replace(/<br>/g, ' '));
</script>
However, the result ends up with an empty paragraph. The two </p></p>
tags in the middle are backwards. It should be </p><p>
and not <p></p>
. It seems as though an opening tag is being inserted even though I have not specified that it should.
This is the result of the above script, with the tags backwards:
<p id="V01-cntntTxt">
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.<p></p>
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.</p>