Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace multiple characters in a string in javascript

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("á","&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>
like image 497
SeinopSys Avatar asked Aug 02 '12 17:08

SeinopSys


People also ask

How do I replace multiple characters in a string?

Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.

How do you replace all occurrences of a character in a string in JavaScript?

To make the method replace() replace all occurrences of the pattern you have to enable the global flag on the regular expression: Append g after at the end of regular expression literal: /search/g. Or when using a regular expression constructor, add 'g' to the second argument: new RegExp('search', 'g')

How do you replace multiple values?

Find and replace multiple values with nested SUBSTITUTE The easiest way to find and replace multiple entries in Excel is by using the SUBSTITUTE function. The formula's logic is very simple: you write a few individual functions to replace an old value with a new one.


2 Answers

You could create an object that has key/value pairs for each character to replace:

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

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

like image 63
jackwanders Avatar answered Nov 15 '22 17:11

jackwanders


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 = {
    "á" : "&aacute;",
    "Á" : "&Aacute;",
    "é" : "&eacute;",
    "É" : "&Eacute;",
    ...
}
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.

like image 32
Bergi Avatar answered Nov 15 '22 16:11

Bergi