How to replace strings with javascript?

Posted by Damiano on Stack Overflow See other posts from Stack Overflow or by Damiano
Published on 2010-06-17T10:15:24Z Indexed on 2010/06/17 12:53 UTC
Read the original article Hit count: 297

Filed under:

Hello everybody,

I have this function:

function emoticons(text){
    var url = "http://www.domain.it/images/smilies/";
    var emt = {
       ":D"  : 'icon_e_biggrin.gif',
       ":-D" : 'icon_e_biggrin.gif',       
       ":)"  : 'icon_e_smile.gif',
       ":-)" : 'icon_e_smile.gif',       
       ";)"  : 'icon_e_wink.gif',
       "';-)" : 'icon_e_wink.gif',

       ":("  : 'icon_e_sad.gif',
       ":-(" : 'icon_e_sad.gif',
       ":o"  : 'icon_e_surprised.gif',
       ":?"  : 'icon_e_confused.gif',
       "8-)" : 'icon_cool.gif',

       ":x"  : 'icon_mad.gif',
       ":P"  : 'icon_razz.gif'
    };

    for (smile in emt){        
        text   = text.replace(smile, '<img src="' + url + emt[smile] + '" class="emoticons" />');
    }

    return (text);
}

As you know .replace() convert the first occurence, how to replace more then one emoticon inside the text? How to change this function?

Thank you very much!

© Stack Overflow or respective owner

Related posts about JavaScript