2011-08-28 80 views
0

我想用我的「替換」列表替換textarea的內容。 ,以便「Hi 23」將成爲「\ u041D \ u0456 23」。我的錯誤是什麼?jQuery替換數組

$("textarea#comment").val(function(i, val) { 
return val.replace(/*the first letter (e.g. A)*/g, "*the second row e.g. \u0410*"); 
}); 

replacements = { 
    "A": "\u0410", 
    "B": "\u0412", 
    "C": "\u0421", 
    "H": "\u041D", 
    "I": "\u0406", 
    "J": "\u0408", 
    "M": "\u041C", 
    "O": "\u041E", 
    "P": "\u0420", 
    "S": "\u0405", 
    "T": "\u0422", 
    "V": "\u0474", 
    "Y": "\u04AE", 

    "a": "\u0430", 
    "c": "\u0441", 
    "e": "\u0435", 
    "h": "\u04BB", 
    "i": "\u0456", 
    "j": "\u0458", 
    "l": "\u04C0", 
    "o": "\u043E", 
    "p": "\u0440", 
    "s": "\u0455", 
    "y": "\u0443", 
    "v": "\u0475"; 
} 

回答

2
var replacements = { 
    "A": "\\u0410", 
    "B": "\\u0412", 
    "C": "\\u0421", 
    "H": "\\u041D", 
    "I": "\\u0406", 
    "J": "\\u0408", 
    "M": "\\u041C", 
    "O": "\\u041E", 
    "P": "\\u0420", 
    "S": "\\u0405", 
    "T": "\\u0422", 
    "V": "\\u0474", 
    "Y": "\\u04AE", 

    "a": "\\u0430", 
    "c": "\\u0441", 
    "e": "\\u0435", 
    "h": "\\u04BB", 
    "i": "\\u0456", 
    "j": "\\u0458", 
    "l": "\\u04C0", 
    "o": "\\u043E", 
    "p": "\\u0440", 
    "s": "\\u0455", 
    "y": "\\u0443", 
    "v": "\\u0475" 
} 

$("textarea#comment").val(function(i, val) 
{ 
    val = val.split(''); 

    $.each(val, function(i,e){ 
     val[i] = replacements[e] ? replacements[e] : e; 
    }); 

    return val.join(''); 
}); 

http://jsfiddle.net/6A2JG/