Regular expression parsing with text replacement

It is required to replace the expression in some html code

|img id="test" class="test2" alt="test3"| 

per expression

|div id="test" class="test2"| test3 |/div|

test1, test2, test3 – any text that will wrap into a div. Perhaps somehow with the help of the exec() function.

If possible, the equivalent would be in php for preg_replace(). Well, I would like to understand the option of converting the second expression into the first.


Answer 1, authority 100%

var str = '|img id="test" class="test2" alt="test3"|';
alert(str.replace(/\|img([^\|]*)alt="([^"]*)"([^\|]*)\|/, '|div$1|$2|/div|'));

Answer 2

string.replace()is what you are looking for.