Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace function returns input

Status
Not open for further replies.

phobe62

Programmer
Jan 30, 2004
22
US
Hi. The following function is returning exactly what is passed to it. For example, if I call formatInput('123456string') it just returns 123456 in the alert box. Does anyone know why this might be happening?

////////////////////////////////////////////
function formatInput(string) {
newstring = string;
newstring.replace("1","-|@|-");
newstring.replace("2","-|#|-");
newstring.replace("3","-|$|-");
newstring.replace("4","-|%|-");
newstring.replace("5","-|^|-");
newstring.replace("6","-|&|-");
newstring.replace("7","-|*|-");
newstring.replace("8","-|(|-");
newstring.replace("9","-|)|-");
newstring.replace("0","-|!|-");
newstring.replace("Z","~!A!~");
newstring.replace("z","~!a!~");
newstring.replace("Y","~!B!~");
newstring.replace("y","~!b!~");
newstring.replace("X","~!C!~");
newstring.replace("x","~!c!~");
newstring.replace("W","~!D!~");
newstring.replace("w","~!d!~");
newstring.replace("V","~!E!~");
newstring.replace("v","~!e!~");
newstring.replace("U","~!F!~");
newstring.replace("u","~!f!~");
newstring.replace("T","~!G!~");
newstring.replace("t","~!g!~");
newstring.replace("S","~!H!~");
newstring.replace("s","~!h!~");
newstring.replace("R","~!I!~");
newstring.replace("r","~!i!~");
newstring.replace("Q","~!J!~");
newstring.replace("q","~!j!~");
newstring.replace("P","~!K!~");
newstring.replace("p","~!k!~");
newstring.replace("O","~!L!~");
newstring.replace("o","~!l!~");
newstring.replace("N","~!M!~");
newstring.replace("n","~!m!~");
newstring.replace("M","~!N!~");
newstring.replace("m","~!n!~");
newstring.replace("L","~!O!~");
newstring.replace("l","~!o!~");
newstring.replace("K","~!P!~");
newstring.replace("k","~!p!~");
newstring.replace("J","~!Q!~");
newstring.replace("j","~!q!~");
newstring.replace("I","~!R!~");
newstring.replace("i","~!r!~");
newstring.replace("H","~!S!~");
newstring.replace("h","~!s!~");
newstring.replace("G","~!T!~");
newstring.replace("g","~!t!~");
newstring.replace("F","~!U!~");
newstring.replace("f","~!u!~");
newstring.replace("E","~!V!~");
newstring.replace("e","~!v!~");
newstring.replace("D","~!W!~");
newstring.replace("d","~!w!~");
newstring.replace("C","~!X!~");
newstring.replace("c","~!x!~");
newstring.replace("B","~!Y!~");
newstring.replace("b","~!y!~");
newstring.replace("A","~!Z!~");
newstring.replace("a","~!z!~");
newstring.replace("!","::exc::");
newstring.replace("@","::ats::");
newstring.replace("#","::num::");
newstring.replace("$","::dol::");
newstring.replace("%","::per::");
newstring.replace("^","::upa::");
newstring.replace("&","::amp::");
newstring.replace("*","::ast::");
newstring.replace("(","::lpa::");
newstring.replace(")","::rpa::");
alert(newstring);
}
////////////////////////////////////////////
 
There's 2 things immediately wrong that I see with your code. First, the replace method accepts a regexp and a string for the parameters, not 2 strings. Secondly, the replace method doesn't modify the original string, it returns a new modified string. So to code your function like you had intended, you would have to set it up in this format:
Code:
function formatInput(str) {
    newstring = str;
    newstring = newstring.replace(/1/,"-|@|-");
    ...
    ...
    ...
//be sure to use the escape character [b]\[/b] on your special characters
    newstring = newstring.replace(/\)/,"::rpa::");
    alert(newstring);
}

Now..... this will achieve what you were trying to code. However, the logic in this method is flawed. Take for example the number 1. It will be changed to -|@|-, until you hit the replace method for @, and then it will become -|::ats::|-. This will happen many places in your code..... I don't really see an easy way to do this without creating some sort of loop and marking the characters as 'changed' so that they don't get changed more than once.


-kaht

banghead.gif
 

>> First, the replace method accepts a regexp and a string for the parameters, not 2 strings.

I thought that, too - but it can actually be used with just strings - which I was very happy to find out (I'm not too hot with RegExp ;o)

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top