I'm playing around with some contenteditable div's and after a while I'm getting the following code:
<p>
some text
<ul>
<li>A</li>
<li>2</li>
</ul>
another text
</p>
Now I'd like to get valid HTML. So I have to extract the ul
from the p
or in other words close the p
tag before the ul
and open it again after the ul
. Afterwards my code should like like this:
<p>some text</p>
<ul>
<li>A</li>
<li>2</li>
</ul>
<p>another text</p>
How can I achieve this (with jQuery)?
I already tried this (where elem is the jQuery element containing p):
var reg1 = new RegExp('<ul>', 'g');
var reg2 = new RegExp('</ul>', 'g')
elem.html(elem.html().replace(reg1, '</p><ul>').replace(reg2, '</ul><p>'));
This seems to work. But I'm not sure whether this is the best solution. Do you have a better idea?
` tags where it can.
– Shadowen Aug 07 '15 at 14:56