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

大家好,我需要为更简单的东西优化代码,所以尽量避免 for 循环。“var 替换”是一个巨大的 html 代码(包含 100 行带有日期、用户名、用户信息(工具提示)的消息的 div 内容,消息,.....)此代码是我的 chrome 扩展中的一部分。所以我不能做到这一点 php 方面。

4

1 回答 1

1

您可以使用一个巨大的正则表达式,形式为/:\)|:\(|.../g,然后传递一个回调作为替换,在您的查找对象中查找匹配项。

于 2013-01-27T21:26:03.050 回答