在javascript中替换字符串中的多个字符

时间:2012-08-02 17:11:08

标签: javascript html variables cookies replace

我得到了这个很好的代码,我不知道为什么不起作用。为了兼容性,它应该获取文本输入的值并用它的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("á","&aacute;");
var rtz1  = rtz0.replace("Á","&Aacute;");

var rtz2  = rtz1.replace("é","&eacute;");
var rtz3  = rtz2.replace("É","&Eacute;");

var rtz4  = rtz3.replace("í","&iacute;");
var rtz5  = rtz4.replace("Í","&Iacute;");

var rtz6  = rtz5.replace("ö","&ouml;");
var rtz7  = rtz6.replace("Ö","&Ouml;");
var rtz8  = rtz7.replace("ő","&&#337;");
var rtz9  = rtz8.replace("Ő","&#336;");
var rtz10 = rtz9.replace("ó","&oacute;");
var rtz11 = rtz10.replace("Ó","&Oacute;");

var rtz12 = rtz11.replace("ü","&uuml;");
var rtz13 = rtz12.replace("Ü","&Uuml;");
var rtz14 = rtz13.replace("ű","&#369;");
var rtz15 = rtz14.replace("Ű","&#368;");
var rtz16 = rtz15.replace("ú","&uacute;");
var uio = rtz16.replace("Ú","&Uacute;");

//Creates a cookie with the final value (different function)
createCookie('reminder',uio,1500);

document.getElementById('reminder1').style.display = '';
}
</script>

4 个答案:

答案 0 :(得分:9)

您可以为要替换的每个字符创建一个具有键/值对的对象:

var chars = {
    "á" : "&aacute;",
    "Á" : "&Aacute;",
    "é" : "&eacute;",
    "É" : "&Eacute;",
    ...
}

然后在.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 = {
    "á" : "&aacute;",
    "Á" : "&Aacute;",
    "é" : "&eacute;",
    "É" : "&Eacute;",
    ...
}
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/,"&aacute;");
var rtz1  = rtz0.replace(/\u00C1/,"&Aacute;");

var rtz2  = rtz1.replace(/\u00E9/,"&eacute;");
var rtz3  = rtz2.replace(/\u00C9/,"&Eacute;");

var rtz4  = rtz3.replace(/\u00ED/,"&iacute;");
var rtz5  = rtz4.replace(/\u00CD/,"&Iacute;");

var rtz6  = rtz5.replace(/\u00F6/,"&ouml;");
var rtz7  = rtz6.replace(/\u00D6/,"&Ouml;");
var rtz8  = rtz7.replace(/\u00F5/,"&&#337;");
var rtz9  = rtz8.replace(/\u00D5/,"&#336;");
var rtz10 = rtz9.replace(/\u00F3/,"&oacute;");
var rtz11 = rtz10.replace(/\u00D3/,"&Oacute;");

var rtz12 = rtz11.replace(/\u00FC/,"&uuml;");
var rtz13 = rtz12.replace(/\u00DC/,"&Uuml;");
var rtz14 = rtz13.replace(/\u0171/,"&#369;");
var rtz15 = rtz14.replace(/\u0170/,"&#368;");
var rtz16 = rtz15.replace(/\u00FA/,"&uacute;");
var uio = rtz16.replace(/\u00DA/,"&Uacute;");

//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"), "&aacute;");

最好创建一个PPvG或jackwanders所提到的数组,但除此之外至少要重用现有的变量。你可以这样轻松地做到这一点:

qwe  = qwe.replace(new RegExp("á", "g"), "&aacute;");
qwe  = qwe.replace(new RegExp("Á", "g"), "&Aacute;");