In the replace characters script (here: [url]http://javascript.internet.com/forms/replace-characters.html[/url] ) how can I edit the code so that I can make it replace more than one letter. Say I wanted a = z and b = w or something. How can I do that?
@LogicianMay 13.2006 — #In the replace characters script (here: http://javascript.internet.com/forms/replace-characters.html ) how can I edit the code so that I can make it replace more than one letter. Say I wanted a = z and b = w or something. How can I do that?[/QUOTE]That script needs re-writing just to do what it does already.
Pretty much only a student could need such a routine, so you may want to check-out regular expressions:
@phpnoviceMay 13.2006 — #...or a sombody wanting to play at secret messages. ? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Transliterate</title> <script type="text/javascript"> <!--// String.prototype.transliterate = function(strFromChars, strIntoChars) { if(typeof strFromChars != "string" || typeof strIntoChars != "string" || strFromChars.length != strIntoChars.length) { return undefined; // must be equal-length strings } strFromChars = strFromChars.toLowerCase(); strIntoChars = strIntoChars.toLowerCase(); var ary = this.toLowerCase().split(""); var p, x, len = ary.length; for(x=0; x<len; ++x) { if(0 <= (p=strFromChars.indexOf(ary[x]))) { ary[x] = strIntoChars.substr(p,1); } } return ary.join(""); } String.prototype.scramble = function() { return this.split("").sort(function(){return Math.random()-.49999999}).join(""); } //--> </script> </head>
<body>
<form action="" onsubmit="return false"> <p><input type="text" name="T1" size="26" value="abcdefghijklmnopqrstuvwxyz"> <input type="button" value="Scramble" onclick="T2.value=T1.value.scramble(); return true;"><br> <input type="text" name="T2" size="26"></p> <p><textarea rows="4" name="S1" cols="35">Now is the time for all young men to come to the aid of their country. The quick brown fox jumped over the lazy dog.</textarea></p> <p><input type="button" value="Encode" onclick="S1.value=S1.value.transliterate(T1.value, T2.value); return true;"> <input type="button" value="Decode" onclick="S1.value=S1.value.transliterate(T2.value, T1.value); return true;"></p> </form>