/    Sign up×
Community /Pin to ProfileBookmark

Replace Characters Script

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?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@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:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference?bjects:RegExp
Copy linkTweet thisAlerts:
@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>

<p> </p>

</body>
</html>
×

Success!

Help @CatMarieS spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...