replacing characters like perl's s///

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

is there somethins similar to perl's s///?
I want to make a client side script that will search a string the user provides and (onclick) 'encode it' by replacing characters.

Mark Hensler
If there is no answer on Google, then there is no question.

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Max,

string.indexOf(searchStr) provides the starting index of the string you are looking for.

Insofar as encoding: I don't recall of any such javascript methods, so you would have to write your own. The simpliest one I can think of is to up the character that is -- if character equals A then replace it with B. However, that means that the encrypting algorythm would be visible to anyone who decided to rightClick.
My recommendation: use perl or some other cgi friendly language.
Vinny

Where the world once stood
the blades of grass cut me still

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I don't care if anyone see's the code... I'm trying to make a client side URL-encoder.

could I do something like...

String = "AaBxCcDc"  // will come from a text box
num = String.indexOf(x)

before = String.substring(0, num-1)
after = String.substring(num+1, String.length)

form.textbox.value = before + "b" + after
'

I had a more complex (but incomplete) version written earlier this week, but then I deleted it thinking that there HAD TO BE an easier way.

Mark Hensler
If there is no answer on Google, then there is no question.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I guess I didn't delete it... I found it.

I started working on it again this morning, but I'm a bit fried and can't find what's wrong. It doesn't work right now with the comments in, and it needs to work with them out (so the whole script can function).

<html>
<body>

&lt;script language=javascript&gt;
<!--

Character = new Array
Character[0] = " "
Character[1] = "/"
Character[2] = "?"

Encoded = new Array
Encoded[0] = "%20"
Encoded[1] = "%2F"
Encoded[2] = "%3F"

function Encode() {

msg = URLencode.message.value

if (msg == "") {
alert("There is nothing to encode.\nThe text box is empty.")
return false
}

for (i=0; i<2; i++) {
CheckFor(msg, i)
} //End for (i=0; i<2; i++)

document.URLencode.message.value = msg
} //End Encode()


function CheckFor(msg, x) {

if (msg.indexOf(Character(x)) > -1) {
/*
Replace(msg, x, msg.indexOf(Character(x)))
*/
} //End if (msg.indexOf(Character(x))
else {
return true
} //End if (msg.indexOf(Character(x)) > -1)

} //End function CheckFor(msg, x)

/*
function Replace(msg, y, pos) {

if (pos == "1") {
before = ""
after = msg.substring(pos+1, msg.length)
}

if (pos == msg.length) {
before = msg.substring(0, pos-1)
after= ""
}

if ((pos != "0") && (pos != msg.length)) {
before = msg.substring(0,pos-1)
after = msg.substring(pos+1, msg.length)
}

msg = before + "Encoded(y)" + after

CheckFor(msg, y) //Any more?
} //End function Replace(msg, y, pos)
*/

//-->
&lt;/script&gt;

<form name=URLencode onSubmit="Encode(); return false;">
<input name=message type=text size=40>
<input type=button value="Encode It!" onClick="Encode()">
</form>

</body>
</html>
'

Mark Hensler
If there is no answer on Google, then there is no question.

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Max,

Sorry for the delay. I can blame the system being down, but...
In my original post, I thought you were attempting to encrypt an entire message.
If you are trying to encode for email/cgi, this is all you need:
endcodedMsg = escape(document.URLencode.message.value);
decoding would be:
decodedMsg = unescape(encodedMsg);
Vinny

Where the world once stood
the blades of grass cut me still

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Thanks!

I didn't know there was a function like that.

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Max,

You're welcome.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.