我得到了这个很好的代码,我不知道为什么不起作用。为了兼容性,它应该获取文本输入的值并用它的HTML代码替换每个给定的国家字符。但是,当我单击按钮时,该函数返回字符串而不做任何更改。 有什么想法吗?
(jsfiddle)
<a id="reminder1" onclick="document.getElementById('reminder2').style.display = ''; document.getElementById('reminder1').style.display = 'none';">
Set reminder
</a>
<a id="reminder2" class="reminder" style="display:none;">
<input type="text" id="reminderh" size=40 style="font-size:20px;">
<input type="button" value="Set" onclick="csere(document.getElementById('reminderh').value);">
</a>
<script>
function csere(qwe){
document.getElementById('reminder2').style.display = 'none';
var rtz0 = qwe.replace("á","á");
var rtz1 = rtz0.replace("Á","Á");
var rtz2 = rtz1.replace("é","é");
var rtz3 = rtz2.replace("É","É");
var rtz4 = rtz3.replace("í","í");
var rtz5 = rtz4.replace("Í","Í");
var rtz6 = rtz5.replace("ö","ö");
var rtz7 = rtz6.replace("Ö","Ö");
var rtz8 = rtz7.replace("ő","&ő");
var rtz9 = rtz8.replace("Ő","Ő");
var rtz10 = rtz9.replace("ó","ó");
var rtz11 = rtz10.replace("Ó","Ó");
var rtz12 = rtz11.replace("ü","ü");
var rtz13 = rtz12.replace("Ü","Ü");
var rtz14 = rtz13.replace("ű","ű");
var rtz15 = rtz14.replace("Ű","Ű");
var rtz16 = rtz15.replace("ú","ú");
var uio = rtz16.replace("Ú","Ú");
//Creates a cookie with the final value (different function)
createCookie('reminder',uio,1500);
document.getElementById('reminder1').style.display = '';
}
</script>
答案 0 :(得分:9)
您可以为要替换的每个字符创建一个具有键/值对的对象:
var chars = {
"á" : "á",
"Á" : "Á",
"é" : "é",
"É" : "É",
...
}
然后在.replace
电话中使用一项功能:
var uio = qwe.replace(/[áÁéÉ]/g,function(c) { return chars[c]; });
您的对象和正则表达式显然需要增长以包含您要替换的所有字符
答案 1 :(得分:9)
您可以通过编程方式replace编辑所有内容,而不是使用命名实体:
return input.replace(/[^ -~]/g, function(chr) {
// ^^^^^^
// this is a regexp for "everything than printable ASCII-characters"
// and even works in a ASCII-only charset. Identic: [^\u0020-\u007E]
return "&#"+chr.charCodeAt(0)+";";
});
如果你想使用命名实体,可以将它与键值映射结合起来(就像在@jackwanders中一样):
var chars = {
"á" : "á",
"Á" : "Á",
"é" : "é",
"É" : "É",
...
}
return input.replace(/[^ -~]/g, function(chr) {
return (chr in chars)
? chars[chr]
: "&#"+chr.charCodeAt(0)+";";
});
但是,您永远不需要在JavaScript中使用html实体。使用UTF8作为所有内容的字符编码,它将起作用。
答案 2 :(得分:3)
字符受HTML页面,JavaScript页面和HTTP请求的编码限制。尝试使用其Unicode等效替换字符:
<a id="reminder1" onclick="document.getElementById('reminder2').style.display = ''; document.getElementById('reminder1').style.display = 'none';">
Set reminder
</a>
<a id="reminder2" class="reminder" style="display:none;">
<input type="text" id="reminderh" size=40 style="font-size:20px;">
<input type="button" value="Set" onclick="csere(document.getElementById('reminderh').value);">
</a>
<script>
function csere(qwe){
document.getElementById('reminder2').style.display = 'none';
var rtz0 = qwe.replace(/\u00E1/,"á");
var rtz1 = rtz0.replace(/\u00C1/,"Á");
var rtz2 = rtz1.replace(/\u00E9/,"é");
var rtz3 = rtz2.replace(/\u00C9/,"É");
var rtz4 = rtz3.replace(/\u00ED/,"í");
var rtz5 = rtz4.replace(/\u00CD/,"Í");
var rtz6 = rtz5.replace(/\u00F6/,"ö");
var rtz7 = rtz6.replace(/\u00D6/,"Ö");
var rtz8 = rtz7.replace(/\u00F5/,"&ő");
var rtz9 = rtz8.replace(/\u00D5/,"Ő");
var rtz10 = rtz9.replace(/\u00F3/,"ó");
var rtz11 = rtz10.replace(/\u00D3/,"Ó");
var rtz12 = rtz11.replace(/\u00FC/,"ü");
var rtz13 = rtz12.replace(/\u00DC/,"Ü");
var rtz14 = rtz13.replace(/\u0171/,"ű");
var rtz15 = rtz14.replace(/\u0170/,"Ű");
var rtz16 = rtz15.replace(/\u00FA/,"ú");
var uio = rtz16.replace(/\u00DA/,"Ú");
//Creates a cookie with the final value (different function)
createCookie('reminder',uio,1500);
document.getElementById('reminder1').style.display = '';
}
</script>
仔细检查我的转化以确定。我在Wikibooks上使用了网格。
答案 3 :(得分:1)
我认为你只是替换了第一个角色的实例。在javascript中,您必须使用正则表达式来指定全局替换:
var rtz0 = qwe.replace(new RegExp("á", "g"), "á");
最好创建一个PPvG或jackwanders所提到的数组,但除此之外至少要重用现有的变量。你可以这样轻松地做到这一点:
qwe = qwe.replace(new RegExp("á", "g"), "á");
qwe = qwe.replace(new RegExp("Á", "g"), "Á");