Question

function changeText(getString){
    var smiles_from_to = new Array();
    smiles_from_to[":)"] = "ab"; smiles_from_to[":-)"] = "ab";
    smiles_from_to[":("] = "ac"; smiles_from_to[":-("] = "ac";
    smiles_from_to["B)"] = "af"; smiles_from_to["B-)"] = "af";
    smiles_from_to[";("] = "crygirl2"; smiles_from_to[";-("] = "crygirl2";
    smiles_from_to[":-*"] = "aw"; smiles_from_to[":*"] = "aw";
    smiles_from_to[":D"] = "ag"; smiles_from_to[":-D"] = "ag";
    smiles_from_to["(devil)"] = "aq"; smiles_from_to["(wtf)"] = "ai";
    smiles_from_to["(sos)"] = "bc"; smiles_from_to["(boom)"] = "bb";
    smiles_from_to["(rofl)"] = "bj"; smiles_from_to["xD"] = "bj";
    smiles_from_to["(music)"] = "ar"; smiles_from_to["(angel)"] = "aa";
    smiles_from_to["(beer)"] = "az"; smiles_from_to["(omg)"] = "bu";
    smiles_from_to["(dance)"] = "bo"; smiles_from_to["(idiot)"] = "bm";
    smiles_from_to["(clap)"] = "bi"; smiles_from_to["(gotkiss)"] = "as";
    var replaced = getString;
    for(var key in smiles_from_to){
       replaced = replaced.replace(key, "<img src='"+chrome.extension.getURL("images/"+smiles_from_to[key]+".gif")+"' />");
    }
    return replaced;
}

Hi everyone ,I need to optimize code for something more simple, so try to avoid for loop.. "var replaced" is a huge html code (content of div that contains 100 lines of messages with date, username, userinfo(tooltip), message,.....) This code is a piece from my chrome extension. so i cant do it php side.

Was it helpful?

Solution

You can use a single giant regex, of the form /:\)|:\(|.../g, then pass a callbacka as the replacement that looks up the match in your lookup object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top