Itay Alon Itay Alon - 7 months ago 42
Javascript Question

replace img tag with alt using javascript

I'm trying to use Regex, but used it only in PHP and years ago:

var msgText = thisMSG.getElementsByClassName('message-text')[0].getElementsByClassName('emojitext')[0].innerHTML;
msgText = msgText.replace('/<img src="([\.]*)"([\.]*)alt="[([\.]*)">/g','\\3');


I want to replace the img tag with the alt content:

<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="emoji emojiordered0186 selectable-text" draggable="false" alt="❤">


any idea what wrong?

edit: the image can be a part of a sentnce, like:

I <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="emoji emojiordered0186 selectable-text" draggable="false" alt="❤"> U!

Answer

Ok, got it:

var msgText = null;
while ( msgText = thisMSG.querySelector('.message-text .emojitext .emoji') )
{
  msgText.outerHTML = msgText.alt;
}

and for use the text:

thisMSG.querySelector('.message-text .emojitext').innerHTML