I am working on a text editor and when the page renders I am trying to convert all <div> tag in to <p> tag if there is no style attribute in the tag using following javascript regex. (Soruce html at the end)
// if <div> has any attribute then do not convert in <p> tag
str = str.replace(/<div>([\w\W]*?)<\/div>/gi, '<p>$2</p>');
But the output of this regx is weird
OUTPUT HTML: Notice the unexpected '<p>$2</p>' at the end and Zoltan and congratulation text goes missing.
<br>
@abc Semantics
<br>
<a href="http://abc.com">http://abc.com</a>
<br>
xxxx5555
<br>
Mail address:
<a target="blank" href="/abc-abc-semantics">@abc</a>
<br>
<br>
<b>
Sub:
</b>
<br>
<br>
Hi abc, <p>$2</p><p>$2</p>;<p>$2</p><p>$2</p>
//Original HTML on which I am applying the regx
<br>
@abc Semantics
<br>
<a href="http://abc.com">http://abc.com</a>
<br>
xxxx5555
<br>
Mail address: <a href="/abc-abc-semantics" target="blank">@abc</a>
<br>
<br>
<b> Sub: </b>
<br> <br>
Hi abc,
<div><br></div>
<div>congratulations :D</div>
<div><br></div>
;
<div>Zoltan</div>
There is no
group 2in yourregex, so$2is taken as$2only, and no replacement it done.You need to use
$1: –See demo on http://fiddle.re/v0jb