我有一个包含emo符号和每个符号对应的图像文件路径的数组。
但是,使用这段代码,只有 :) 这个符号才能返回正确的smile.png图像,其他的emo没有起作用。
我该如何编写正确的正则表达式来匹配每个符号并为每个emo选择适当的文件? 在JSFIDDLE上查看我的代码演示
但是,使用这段代码,只有 :) 这个符号才能返回正确的smile.png图像,其他的emo没有起作用。
我该如何编写正确的正则表达式来匹配每个符号并为每个emo选择适当的文件? 在JSFIDDLE上查看我的代码演示
//Replace Emo with images
function replaceEmoticons(text) {
var emoticons = {
':)' : 'smile.png',
': )' : 'smile.png',
':D' : 'laugh.png',
':->' : 'laugh.png',
':d' : 'laugh-sm.png',
':-)': 'smile-simple.png',
':p': 'tounge.png',
':P': 'tounge-lg.png',
': P': 'tng1.png',
'>-)': 'evil.png',
':(': 'sad.png',
':-(': 'sadd.png',
':-<': 'sadd.png',
':-O': 'surprise.png',
':O': 'sur2.png',
':o': 'sur3.png',
':-o': 'sur3.png',
':-*': 'kiss.png',
':*': 'kiss.png',
':-@': 'angry.png',
':@': 'angry.png',
':$': 'con2.png',
':-$': 'con1.png',
'O.o': 'con2.png',
'o.O': 'con2.png',
':/': 'weird.png',
':x': 'x.png',
':X': 'x.png',
':!': 's.png',
'(:I': 'egg.png',
'^.^': 'kit.png',
'^_^': 'kit.png',
';)': 'wink.png',
';-)': 'wink.png',
":')": 'hc.png',
":'(": 'cry.png',
"|-O": 'yawn.png',
"-_-": 'poke.png',
":|": 'p1.png',
"$_$": 'he.png'
}, url = "images/emo/";
// a simple regex to match the characters used in the emoticons
return text.replace(/[:\-)D]+/g, function (match) {
return typeof emoticons[match] != 'undefined' ?
'<img class="emo" src="'+url+emoticons[match]+'"/>' :
match;
});
}
replaceEmoticons("Hi this is a test string :) with all :P emos working :D");