I have a text area for comments and a division element that displays the comment preview. I want to convert emoticons into images within the preview. While some emoticons are rendering correctly, the following ones are not: ‘angel’, ‘arrow’, ‘at’, ‘:-]’, ‘:-?’, ‘B-)’, ‘B)’, ‘;)’, ‘exclamation’, ‘<3’, ‘huh’, ‘my’, ‘roll’, ‘:(’, ‘shy’, ‘|-)’, ‘:-P’. I’m puzzled about why these specific emoticons fail to display as images. How can I fix this? Here’s my JavaScript code:
var emoticons = {
':)': '<img src="images/smile.png" alt="" />',
':-)': '<img src="images/smile.png" alt="" />',
':D': '<img src="images/smile.png" alt="" />',
':-(': '<img src="images/sad.png" alt="" />',
'angel': '<img src="images/angel.png" alt="" />',
'arrow': '<img src="images/arrow.png" alt="" />',
'at': '<img src="images/@.png" alt="" />',
':-D': '<img src="images/grin.png" alt="" />',
'lol': '<img src="images/laugh.png" alt="" />',
':-]': '<img src="images/blush.png" alt="" />',
':-?': '<img src="images/confused.png" alt="" />',
'B-)': '<img src="images/cool.png" alt="" />',
'B)': '<img src="images/cool.png" alt="" />',
';)': '<img src="images/wink.png" alt="" />',
'exclamation': '<img src="images/exclamation.png" alt="" />',
'<3': '<img src="images/heart.png" alt="" />',
'huh': '<img src="images/huh.png" alt="" />',
'my': '<img src="images/my.png" alt="" />',
'roll': '<img src="images/roll.png" alt="" />',
':(': '<img src="images/sad.png" alt="" />',
'shy': '<img src="images/shy.png" alt="" />',
'|-)': '<img src="images/sleepy.png" alt="" />',
':-P': '<img src="images/tongue.png" alt="" />',
':-|': '<img src="images/neutral.png" alt="" />',
';-)': '<img src="images/wink.png" alt="" />'
};
$(document).ready(function() {
$('.comments > textarea').on('input', function() {
$('.comments > div').html(parseEmojis($('.comments > textarea').val()));
});
});
function parseEmojis(message) {
message = message.replace(/(?:\r\n|\r|\n)/g, '<br />');
return message.replace(/(\:\)|\:-\)|\:D|\:-D|\blol\b|\:-\||\:-\(|\;\-\))/g, function(match) {
return emoticons[match] || match;
});
}