简体   繁体   English

用他们的图像替换表情符号列表

[英]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>标记结果应为:

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

(I have to concatenate the url varible to the name of the image). (我必须将url varible连接到图像的名称)。

THank you very much! 非常感谢你!

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. 编辑: @ pepkin88提出了一个非常好的建议,根据emoticons对象的属性名称构建正则表达式。

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 | 转义模式存储在一个数组中,后来用于使用RegExp构造函数构建正则表达式,基本上连接所有使用|分隔的模式| 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 :)');
for ( smile in emoticons )
{
   text = text.replace(smile, '<img src="' + url + emoticons[smile] + '" />');
}

Using a regex with an array of find replace elements works well. 使用带有find替换元素数组的正则表达式效果很好。

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'), '<img src="emotes/' + emotes[i][1] + '">');
    }
    return body;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM