简体   繁体   中英

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

use |

you could do something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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