Javascript 将表情符号列表替换为其图像

Javascript 将表情符号列表替换为其图像,javascript,emoticons,Javascript,Emoticons,我有一个数组,其中包含: emoticons = { ':-)' : 'smile1.gif', ':)' : 'smile2.gif', ':D' : 'smile3.gif' } 然后我对文本有一个变量 var text = 'this is a simple test :)'; 和一个带有网站url的变量 var url = "http://www.domain.com/"; 如何编写用符号的图像替换符号的函数 (我必须将url变量连接到图像的名称

我有一个数组,其中包含:

emoticons = {
   ':-)' : 'smile1.gif',
   ':)'  : 'smile2.gif',
   ':D'  : 'smile3.gif'     
}
然后我对文本有一个变量

var text = 'this is a simple test :)';
和一个带有网站url的变量

var url = "http://www.domain.com/";
如何编写用符号的图像替换符号的函数


(我必须将url变量连接到图像的名称)

多谢各位

for(表情符号中的微笑)
<img src="http://www.domain.com/simple2.gif" />
{ text=text.replace(微笑“”); }
另一种方法:

for ( smile in emoticons )
{
   text = text.replace(smile, '<img src="' + url + emoticons[smile] + '" />');
}

使用带有find-replace元素数组的正则表达式效果很好

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 :)');
var-emotes=[
[':\\)','happy.png'],
[':\\\(','sad.png']
];
函数ApplyMotesFormat(正文){
对于(变量i=0;i
replace只替换匹配字符串的第一次出现,因此无法正常工作。它只用于替换每个笑脸的第一次出现。在类似“This is replaced:)但不是This:)”的字符串中,第二个笑脸保持不变。另外,请确保在
for…In
语句中使用
var
,否则,如果代码在未声明该范围内的
smile
变量的函数中,它将成为全局变量,在循环中使用
if(emoticons.hasOwnProperty(smile))
是一个好主意。好的,那么如何使用.replace()要替换多个事件?要替换多个事件,请将
smile
更改为
new RegExp(smile,'g')
如果根据
emotics
@pepkin88:很好的建议:)中的键值生成RegExp,效果会更好,我添加了一个函数来实现这一点。关闭
replace()
(类似于…)可以进一步增强这一点-这将加速对函数的重复调用。@CMS这对我的一个应用程序帮助很大。@redV我很高兴它帮助了您!
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 :)');
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;
}