Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a list of emoticons with their images

I have an array with:

emoticons = {
   ':-)' : 'smile1.gif',
   ':)'  : 'smile2.gif',
   ':D'  : 'smile3.gif'     
}

then i have a variabile with the text.

var text = 'this is a simple test :)';

and a variable with the url of the website

var url = "http://www.domain.com/";

How to write a function that replace the symbols with their images?

The <img> tag result should be:

<img src="http://www.domain.com/simple2.gif" />

(I have to concatenate the url varible to the name of the image).

THank you very much!

like image 593
Damiano Avatar asked Jun 16 '10 17:06

Damiano


People also ask

How do you copy emoticons?

Click on any emoji - and it would be copied to your clipboard automatically. You can now paste it anywhere using the usual keyboard command CTRL + V, or the "Paste" option present in the context menu (right click menu).

Are emojis and emoticons the same?

So, if you come across a smiley face that contains a character you can find on your computer keyboard, it's an emoticon. If it's a little cartoon figure that is free from the binds of punctuation, numbers, and letters, it's an emoji.

How many emoticons are there?

👉 In total there are 3,633 emojis in the Unicode Standard, as of September 2021. The most recent emoji release is Emoji 14.0, which added 112 new emojis. This figure includes sequences for gender, skin tone, flags, and the components that are used to create keycap, flag, and other sequences.


1 Answers

Another approach:

function replaceEmoticons(text) {
  var emoticons = {
    ':-)' : 'smile1.gif',
    ':)'  : 'smile2.gif',
    ':D'  : 'smile3.gif'
  }, url = "http://www.domain.com/";
  // a simple regex to match the characters used in the emoticons
  return text.replace(/[:\-)D]+/g, function (match) {
    return typeof emoticons[match] != 'undefined' ?
           '<img src="'+url+emoticons[match]+'"/>' :
           match;
  });
}

replaceEmoticons('this is a simple test :)');
// "this is a simple test <img src="http://www.domain.com/smile2.gif"/>"

Edit: @pepkin88 made a really good suggestion, build the regular expression based on the property names of the emoticons object.

It can be easily done, but we have to escape meta-characters if we want this to work properly.

The escaped patterns are stored on an array, that is later used to build the regular expression using the RegExp constructor, by basically joining all the patterns separated with the | metacharacter.

function replaceEmoticons(text) {
  var emoticons = {
    ':-)' : 'smile1.gif',
    ':)'  : 'smile2.gif',
    ':D'  : 'smile3.gif',
    ':-|'  : 'smile4.gif'
  }, url = "http://www.domain.com/", patterns = [],
     metachars = /[[\]{}()*+?.\\|^$\-,&#\s]/g;

  // build a regex pattern for each defined property
  for (var i in emoticons) {
    if (emoticons.hasOwnProperty(i)){ // escape metacharacters
      patterns.push('('+i.replace(metachars, "\\$&")+')');
    }
  }

  // build the regular expression and replace
  return text.replace(new RegExp(patterns.join('|'),'g'), function (match) {
    return typeof emoticons[match] != 'undefined' ?
           '<img src="'+url+emoticons[match]+'"/>' :
           match;
  });
}

replaceEmoticons('this is a simple test :-) :-| :D :)');
like image 156
Christian C. Salvadó Avatar answered Oct 18 '22 04:10

Christian C. Salvadó