I have some text content that I am capturing from a third-party source, and which sometimes contains emoji, represented as image elements. I find each of the emoji image elements, and convert them to the unicode character for that emoji using the following code:
$(this).find('img.emoji').each(function(i){
emoji = decodeURIComponent($(this).data('textvalue'));
$(this).replaceWith(emoji);
});
However, the text immediately preceding each emoji image element contains an extra whitespace character, right before the emoji. See:
'[...] blah blah blah <img class="emoji" data-textvalue="%F0%9F%98%92">'
but it should be:
'[...] blah blah blah <img class="emoji" data-textvalue="%F0%9F%98%92">'
Because this is coming from a third-party source, I have no control over the original copy. But, I would like to remove that extra whitespace character in each instance of an emoji image (whether before or after converting it to unicode doesn't matter, but I suspect it may be easier to do before). How do I accomplish this?
One idea I had was to possibly get the character location of the beginning of the image element using javascript's str.indexOf
, and then delete the character that was 1 less than that. But that would require converting the parent element to a string, and would cause problems if the intial text itself contained the phrase "<img"
, as unlikely as that would be.
Is there an easy way to do this that I am missing?