正则表达式匹配各种表情符号的列表 [英] Regex matching list of emoticons of various type

查看:1269
本文介绍了正则表达式匹配各种表情符号的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个功能,使我能够解析文本以寻找与格式不同的表情符号字符串匹配的文本.它可以是:)类型的图释,也可以是&lt;dog&gt;(<dog>)类型. 目前,我的函数可以找到这两种类型,但需要两个不同的正则表达式来完成此任务,我只想一步就可以做到.有什么想法吗?

jsfiddle上的实时示例: http://jsfiddle.net/rmQSx/5/

和代码:

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:):('));

解决方案

我很确定您不需要在每个for循环开始时所做的有趣的事情来修改正则表达式.摆脱它,没有什么可以阻止您将这两个集合合并在一起.

但是,制作正则表达式时,应该在表情符号中转义特殊字符().另外,我建议使正则表达式搜索不区分大小写,以使<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;    
}

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; (<dog>) type. 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/

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. 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.

//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;    
}

这篇关于正则表达式匹配各种表情符号的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆