60734
Goto Top

Mehrere Strings auf einmal ersetzen

Hallo,

ich will mit einer JavaScript Function mehrere Strings innerhalb eines Strings auf einmal ersetzen.

Hier einmal beispielhalber die Funktion:

<script>
function replace_code(str)
{
str = str.replace(/a/g,"hallo");  
str = str.replace(/h/g,"tschuess");  
alert(str);
}
</script>

wenn ich jetzt als string "a h" übergebe, würde ich dann "tschuessallo tschuess" zurückbekommen, weil er ja alles nacheinander ersetzt, ich will aber dass er mir
als rückgabewert "hallo tschuess" liefert.
Also müsste man beides auf einmal ersetzen....

Geht sowas?

Vielen Dank schonmal

Content-Key: 101681

Url: https://administrator.de/contentid/101681

Ausgedruckt am: 29.03.2024 um 13:03 Uhr