I am writing a simple replace string with the help of regex. I want to add a new attribute such as "isElm"
in every <p>
tag. Please refer the below fiddle. If I use regex as /<p>(.*?)<\/p>/g
and use replace, it works, but if the <p>
tag has an attribute, it does not. However, if I use this regex: /<p([^>]*)>(.*?)<\/p>/g
, the replace with attribute works, but without the <p>
tag, it does not work. I want to find a generic solution for this. Below are the two cases:
<p>one</p>
should change to<p isElm="true">one</p>
<p id='2'>two</p>
should change to<p id="2" isElm="true">
Note that for both cases above, the isElm="true"
attribute is added after the replace. I do not want to create a DOM or jQuery object. I have to use regex due to some limitations.
Fiddle Link :- http://jsfiddle.net/9VHtR/1/