2

I am trying to make a emoticon using jquery. But i have a problem with multiple emoji. I have created this demo from jsfiddle.net .

The jquery code is working fine with if user write different multiple emoji but if user write two or more smile like :):):):) then jquery code showing just first emoji other emoji will not showing. Anyone can help me here ?

JS

jQuery.fn.emoticons = function(icon_folder) {
    /* emoticons is the folder where the emoticons are stored*/
    var icon_folder = icon_folder || "emoticons";
    //var settings = jQuery.extend({emoticons: "emoticons"}, options);
    /* keys are the emoticons
     * values are the ways of writing the emoticon
     *
     * for each emoticons should be an image with filename
     * 'face-emoticon.png'
     * so for example, if we want to add a cow emoticon
     * we add "cow" : Array("(C)") to emotes
     * and an image called 'face-cow.png' under the emoticons folder   
     */
    var emotes = {"smile": Array(":-)",":)","=]","=)"),
                  "sad": Array(":-(","=(",":[",":<"),
                  "wink": Array(";-)",";)",";]","*)"),
                  "grin": Array(":D","=D","XD","BD","8D","xD"),
                  "surprise": Array(":O","=O",":-O","=-O"),
                  "devilish": Array("(6)"),
                  "angel": Array("(A)"),
                  "crying": Array(":'(",":'-("),
                  "plain": Array(":|"),
                  "smile-big": Array(":o)"),
                  "glasses": Array("8)","8-)"),
                  "kiss": Array("(K)",":-*"),
                  "monkey": Array("(M)")};

    /* Replaces all ocurrences of emoticons in the given html with images
     */
    function emoticons(html){
        for(var emoticon in emotes){
            for(var i = 0; i < emotes[emoticon].length; i++){
                /* css class of images is emoticonimg for styling them*/
                html = html.replace(emotes[emoticon][i],"<img src=\"http://www.oobenn.com/"+icon_folder+"/face-"+emoticon+".png\" class=\"emoticonimg\" alt=\""+emotes[emoticon][i]+"\"/>","g");
            }
        }
        return html;
    }
    return this.each(function(){
        $(this).html(emoticons($(this).html()));
    });
};

$(document).ready(function(){
    $(".posted").emoticons();
});

1 Answers1

1

To do that, you will need regular expressions as the flags (your ,"g") in replace are deprecated and will not work on every browser (source).

So you need to create your regexp with your smiley code:

var re = new RegExp(emotes[emoticon][i], 'g');

But as your smiley codes has special characters, you need to escape them. To do that, I took a piece of code from this answer:

function escape(smiley){
  return smiley.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

Finaly, you can use replace to replace all your smileys, even when there is several times the same smiley:

var escaped = escape(emotes[emoticon][i]);
var re = new RegExp(escaped, 'g');
html = html.replace(re,"<img src=\"http://www.oobenn.com/"+icon_folder+"/face-"+emoticon+".png\" class=\"emoticonimg\" alt=\""+emotes[emoticon][i]+"\"/>");

Working Demo

Community
  • 1
  • 1
Finrod
  • 2,425
  • 4
  • 28
  • 38
  • Thanks for your best answer. It is very good description for me and i understood what i am missing in the code. Thank you so much again. –  Dec 04 '15 at 15:04