Regex matching list of emoticons of various type

与世无争的帅哥 提交于 2019-12-08 04:02:13

问题


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

回答1:


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



回答2:


use |

you could do something like:

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


来源:https://stackoverflow.com/questions/7317299/regex-matching-list-of-emoticons-of-various-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!