简体   繁体   English

正则表达式匹配各种表情符号的列表

[英]Regex matching list of emoticons of various type

I need to create a faunctionality, that will allow me to parse a text looking for a match for emoticon string in different format. 我需要创建一个功能,这将允许我解析文本以寻找不同格式的图释字符串的匹配项。 It can be either a :) type emoticon, or a &lt;dog&gt; 它可以是:)类型的图释,也可以是&lt;dog&gt; ( <dog> ) type. <dog> )类型。 Currently my function can find both types but needs two different regexes for this task, and I'd like to make it in just one step. 目前,我的函数可以找到这两种类型,但是需要两个不同的正则表达式来完成此任务,我想一步就完成。 Any ideas ? 有任何想法吗 ?

Live example at jsfiddle: http://jsfiddle.net/rmQSx/5/ jsfiddle的实时示例: http : //jsfiddle.net/rmQSx/5/

and the code : 和代码:

function replaceEmoticons(text) {
    var emots1 = {
        ':)': 'smile.gif',
        ':(': 'sad.gif',
    },
    emots2 = {
        '&lt;dog&gt;': 'dog.gif',
        '&lt;fly&gt;': 'fly.gif'   
    },
    regex1,regex2,p,re;


    for(p in emots1){
        regex1 = "["+p+"]{2}";
        re = new RegExp(regex1, "g");

        text = text.replace(re, function (match) {
            return typeof emots1[match] != 'undefined' ?
                '<img src="'+emots1[match]+'"/>' :
                match;
        });
    }

    for(p in emots2){
        regex2 = "("+p+")(|$)";
        re = new RegExp(regex2, "g");

        text = text.replace(re, function(match) {
            return typeof emots2[match] != 'undefined' ?
                '<img src="'+emots2[match]+'"/>' :
                match;
        });
    }

     return text;   
}

console.log(replaceEmoticons('this is &lt;dog&gt;b a&lt;fly&gt; simple test :) , ;)   and more:):('));

I'm pretty sure you don't need the funny stuff you do at the beginning of each for loop to modify the regex. 我很确定您不需要在每个for循环开始时所做的有趣的事情来修改正则表达式。 Get rid of that, and there's nothing stopping you from merging the two sets together. 摆脱它,没有什么可以阻止您将这两个集合合并在一起。

However, you should escape the special characters ( and ) in your smileys when making the regexes. 但是,制作正则表达式时,您应该在笑脸中转义特殊字符() Also, I would suggest making the regex search case-insensitive so that <dog> and <DOG> are both matched. 另外,我建议使正则表达式搜索不区分大小写,以使<dog><DOG>都匹配。

//helper function to escape special characters in regex
function RegExpEscape(text) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
}

function replaceEmoticons(text) {   
    var emots = {
        ':)'         : 'smile.gif',
        ':('         : 'sad.gif',
        '&lt;dog&gt;': 'dog.gif',
        '&lt;fly&gt;': 'fly.gif'
    }

    var result = text;
    var emotcode;
    var regex;

    for (emotcode in emots)
    {
        regex = new RegExp(RegExpEscape(emotcode), 'gi');
        result = result.replace(regex, function(match) {
            var pic = emots[match.toLowerCase()];

            if (pic != undefined) {
                return '<img src="' + pic + '"/>';
                    } else {
                return match;
                    }
                });
    }

    return result;    
}

use | 使用|

you could do something like: 你可以这样做:

re = new RegExp(smiley1|smiley2|...|smileyN);

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

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