Reputation: 8937
I got this nice code, which I have no idea why doesn't work. It should get the value of a text input and replace each given national character with it's HTML code, for compatibility purposes. But, when I click the button, the function returns the string without any changes. Any idea?
(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>
Upvotes: 12
Views: 22411
Reputation: 664405
You can just replace everything programmatically, not using named entities:
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)+";";
});
If you want to use named entities, you can combine this with a key-value-map (as like in @jackwanders answer):
var chars = {
"á" : "á",
"Á" : "Á",
"é" : "é",
"É" : "É",
...
}
return input.replace(/[^ -~]/g, function(chr) {
return (chr in chars)
? chars[chr]
: "&#"+chr.charCodeAt(0)+";";
});
However, you should never need to use html entities in JavaScript. Use UTF8 as the character encoding for everything, and it will work.
Upvotes: 9
Reputation: 28635
I think you are having an issue with only replacing the first instance of a character. In javascript you have to specifiy global replaces using regex like this:
var rtz0 = qwe.replace(new RegExp("á", "g"), "á");
It would be best to create an array as mentioned by PPvG or jackwanders, but otherwise atleast reuse the existing variable. You could easily do it like this:
qwe = qwe.replace(new RegExp("á", "g"), "á");
qwe = qwe.replace(new RegExp("Á", "g"), "Á");
Upvotes: 1
Reputation: 2175
The characters are subject to the encoding of the HTML page, the JavaScript page, and the HTTP request. Try replacing the characters with their Unicode equivalents:
<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>
Double check my conversions to be sure. I used the grid on Wikibooks.
Upvotes: 3
Reputation: 16020
You could create an object that has key/value pairs for each character to replace:
var chars = {
"á" : "á",
"Á" : "Á",
"é" : "é",
"É" : "É",
...
}
And then use a function in your .replace
call:
var uio = qwe.replace(/[áÁéÉ]/g,function(c) { return chars[c]; });
Your object and regular expression will obviously need to grow to include all the characters you want to replace
Upvotes: 11