我实现了匹配表情键组合的 javascript 函数,并将它们替换为 span 和特定的 css 类。
我确信我用所有受支持的表情符号测试了系统,并且所有替换都工作得很好。但是现在有些表情符号无法匹配,我不知道为什么。
例如。表情符号 ':P' 与我的正则表达式不匹配,但 ':)' 匹配。
匹配和替换字符的函数是:
/**
     * Replace combination of chars with emoticons
     * @param rawText string
     * @returns string
     * @private
     */
    chatWindow.prototype._processEmoticons = function(rawText) {
        var fbEmoticonHolder = '<span class="emoticon :HOLDER:"></span>';
        var replacementMap = {
            ':)' : fbEmoticonHolder.replace(':HOLDER:', 'smile'),
            ':-)' : fbEmoticonHolder.replace(':HOLDER:', 'smile'),
            ':(' : fbEmoticonHolder.replace(':HOLDER:', 'sad'),
            ':-(' : fbEmoticonHolder.replace(':HOLDER:', 'sad'),
            '8-)' : fbEmoticonHolder.replace(':HOLDER:', 'geek'),
            '8)' : fbEmoticonHolder.replace(':HOLDER:', 'geek'),
            'B-)' : fbEmoticonHolder.replace(':HOLDER:', 'geek'),
            'B)' : fbEmoticonHolder.replace(':HOLDER:', 'geek'),
            ':-P' : fbEmoticonHolder.replace(':HOLDER:', 'tongue'),
            ':P' : fbEmoticonHolder.replace(':HOLDER:', 'tongue'),
            ':-p' : fbEmoticonHolder.replace(':HOLDER:', 'tongue'),
            ':p' : fbEmoticonHolder.replace(':HOLDER:', 'tongue'),
            '=P' : fbEmoticonHolder.replace(':HOLDER:', 'tongue'),
            '=p' : fbEmoticonHolder.replace(':HOLDER:', 'tongue'),
            'o.O' : fbEmoticonHolder.replace(':HOLDER:', 'speechless'),
            'O.o' : fbEmoticonHolder.replace(':HOLDER:', 'speechless'),
            ':v' : fbEmoticonHolder.replace(':HOLDER:', 'pacman'),
            'O:)' : fbEmoticonHolder.replace(':HOLDER:', 'angel'),
            'O:-)' :  fbEmoticonHolder.replace(':HOLDER:', 'angel'),
            '<3' : fbEmoticonHolder.replace(':HOLDER:', 'heart'),
            ':\'(' : fbEmoticonHolder.replace(':HOLDER:', 'cry'),
            ':-D' : fbEmoticonHolder.replace(':HOLDER:', 'laugh'),
            ':D' : fbEmoticonHolder.replace(':HOLDER:', 'laugh'),
            '=D' : fbEmoticonHolder.replace(':HOLDER:', 'laugh'),
            '8-|' : fbEmoticonHolder.replace(':HOLDER:', 'cool'),
            '8|' : fbEmoticonHolder.replace(':HOLDER:', 'cool'),
            'B-|' : fbEmoticonHolder.replace(':HOLDER:', 'cool'),
            'B|' : fbEmoticonHolder.replace(':HOLDER:', 'cool'),
            ';-)' : fbEmoticonHolder.replace(':HOLDER:', 'wink'),
            ';)' : fbEmoticonHolder.replace(':HOLDER:', 'wink'),
            ':-&' : fbEmoticonHolder.replace(':HOLDER:', 'sick'),
            ':&' : fbEmoticonHolder.replace(':HOLDER:', 'sick'),
            ':-O' : fbEmoticonHolder.replace(':HOLDER:', 'shock'),
            ':O' : fbEmoticonHolder.replace(':HOLDER:', 'shock'),
            ':-o' : fbEmoticonHolder.replace(':HOLDER:', 'shock'),
            ':o' : fbEmoticonHolder.replace(':HOLDER:', 'shock'),
            '3:)' : fbEmoticonHolder.replace(':HOLDER:', 'devil'),
            '3:-)' : fbEmoticonHolder.replace(':HOLDER:', 'devil'),
            ':-/' : fbEmoticonHolder.replace(':HOLDER:', 'mixed'),
            ':/' : fbEmoticonHolder.replace(':HOLDER:', 'mixed'),
            ':-\\' : fbEmoticonHolder.replace(':HOLDER:', 'mixed'),
            ':\\' : fbEmoticonHolder.replace(':HOLDER:', 'mixed'),
            '[[f9.rainbow]]' : fbEmoticonHolder.replace(':HOLDER:', 'rainbow'),
            '[[f9.cake]]' : fbEmoticonHolder.replace(':HOLDER:', 'cake'),
            '[[f9.coffee]]' : fbEmoticonHolder.replace(':HOLDER:', 'coffee'),
            '[[f9.gift]]' : fbEmoticonHolder.replace(':HOLDER:', 'gift'),
            '[[f9.bomb]]' : fbEmoticonHolder.replace(':HOLDER:', 'bomb'),
            '[[f9.clap]]' : fbEmoticonHolder.replace(':HOLDER:', 'clap'),
            '[[f9.sleepy]]' : fbEmoticonHolder.replace(':HOLDER:', 'sleepy'),
            '[[f9.stary]]' : fbEmoticonHolder.replace(':HOLDER:', 'stary'),
            '[[f9.heartbreak]]' : fbEmoticonHolder.replace(':HOLDER:', 'heartbreak'),
            '[[f9.inlove]]' : fbEmoticonHolder.replace(':HOLDER:', 'inlove')
        };
        var charsForReplacement = [
            ':\\)', ':-\\)', ':\\(', ':-\\(', '8-\\)', '8\\)', 'B-\\)', 'B\\)', ':-P', ':P', ':-p', ':p', '=P', '=p', 'o.O',
            'O.o', ':v', 'O:\\)', 'O:-\\)', '<3', ':\'\\(', ':-D', ':D', '=D', '8-\\|', '8\\|', 'B-\\|', 'B\\|', ';-\\)',
            ';\\)', ':-&', ':&', ':-O', ':O', ':-o', ':o', '3:\\)', '3:-\\)', ':-/', ':/', ':-\\\\', ':\\\\',
            '\\[\\[f9.rainbow\\]\\]', '\\[\\[f9.cake\\]\\]', '\\[\\[f9.coffee\\]\\]', '\\[\\[f9.gift\\]\\]',
            '\\[\\[f9.bomb\\]\\]', '\\[\\[f9.clap\\]\\]', '\\[\\[f9.sleepy\\]\\]', '\\[\\[f9.stary\\]\\]',
            '\\[\\[f9.heartbreak\\]\\]', '\\[\\[f9.inlove\\]\\]'
        ];
        /*
         FOR TESTING PURPOSES (paste it in the chat window)
         :) :-) :( :-(
         8-) 8) B-) B) :-P :P :-p :p =P =p o.O O.o :v O:)
         O:-) <3 :'( :-D :D =D 8-| 8| B-| B| ;-) ;) :-& :& :-O :O :-o :o 3:) 3:-) :-/ :/ :-\ :\
         [[f9.rainbow]] [[f9.cake]] [[f9.coffee]] [[f9.gift]] [[f9.bomb]] [[f9.clap]] [[f9.sleepy]] [[f9.stary]] [[f9.heartbreak]] [[f9.inlove]]
         */
        // generate regex - START
        var regexPattern = '\\B(';
        for (var i in charsForReplacement) {
            regexPattern += '(?:' + charsForReplacement[i] + ')';
            if (i != charsForReplacement.length - 1) {
                regexPattern += '|';
            }
        }
        regexPattern += ')\\B(?!\\w)';
        // generate regex - END
        var regex = new RegExp(regexPattern, 'g');
        return rawText.replace(regex, function(match) {
            var replacement = $.trim(match);
            return match.replace(replacement, replacementMap[replacement]);
        });
    };
我要做的是创建正则表达式来匹配所有表情符号。数组 charsForReplacement 用于创建常规 exp。模式,因为表情符号有时包含正则表达式模式中的特殊字符。
当 emoticn 匹配然后读取需要替换的内容时使用 replacementMap。
生成的注册。exp。模式是:
\B((?::))|(?::-))|(?::()|(?::-()|(?:8-))|(?:8))|(? :B-))|(?:B))|(?::-P)|(?::P)|(?::-p)|(?::p)|(?:=P)| (?:=p)|(?:oO)|(?:Oo)|(?::v)|(?:O:))|(?:O:-))|(?:<3)| (?::'()|(?::-D)|(?::D)|(?:=D)|(?:8-\|)|(?:8\|)|(?: B-\|)|(?:B\|)|(?:;-))|(?:;))|(?::-&)|(?::&)|(?::-O )|(?::O)|(?::-o)|(?::o)|(?:3:))|(?:3:-))|(?::-/)|( ?::/)|(?::-\)|(?::\)|(?:[[f9.rainbow]])|(?:[[f9.cake]])|(?:[[ f9.coffee]])|(?:[[f9.gift]])|(?:[[f9.bomb]])|(?:[[f9.clap]])|(?:[[f9.困]])|(?:[[f9.stary]])|(?:[[f9.heartbreak]])|(?:[[f9.inlove]]))\B(?!\w)
但这似乎很复杂,但实际上并非如此。那是因为有很多表情可以匹配。
但是假设我们只想匹配几个表情符号,例如':)'、':P'、'XD' 然后注册。exp。模式看起来像:
\B((?::))|(?::P)|(?:XD))\B(?!\w)
我需要你们的帮助!