【发布时间】:2016-06-19 05:13:32
【问题描述】:
我正在为一个游戏项目开发一个聊天客户端,并且正在实施表情符号。表情符号在聊天中的显示位置的基本规则是,当它们直接位于文本旁边时,它们不会出现。
我创建了正则表达式:\B(emoticontext)\B。
不幸的是,我遇到了一个问题,除了包含字母的表情之外,这对每个表情都非常有效。 (例如:D、O_o 等)
我不知道如何补救这种情况。
function parseEmoticons(text) {
var pattern;
emoticons.forEach(function (emoticon) {
pattern = new RegExp("\\B" + emoticon.string + "\\B", 'g');
text = text.replace(pattern, emoticon.img);
});
return text;
}
这是表情符号数组的一部分,用于上下文。
{ 'string': ':\\)', 'img': '<img src="' + imgpath + 'emoticons/smile.png" class="emoticon"/>' },
{ 'string': ':O', 'img': '<img src="' + imgpath + 'emoticons/surprised.png" class="emoticon"/>' },
{ 'string': ':D', 'img': '<img src="' + imgpath + 'emoticons/happy.png" class="emoticon"/>' },
【问题讨论】:
标签: javascript regex emoticons