Replace a list of emoticons with their images

前端 未结 3 1052
挽巷
挽巷 2020-11-27 14:31

I have an array with:

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

then i hav

3条回答
  •  渐次进展
    2020-11-27 15:21

    Using a regex with an array of find replace elements works well.

    var emotes = [
        [':\\\)', 'happy.png'],
        [':\\\(', 'sad.png']
    ];
    
    function applyEmotesFormat(body){
        for(var i = 0; i < emotes.length; i++){
            body = body.replace(new RegExp(emotes[i][0], 'gi'), '');
        }
        return body;
    }
    

提交回复
热议问题