We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Assist; In dis
Topic Started: Dec 30 2004, 12:58 PM (513 Views)
purge.exe
Member
[ *  *  *  * ]
Replace function
 
function replace_first(from, to, varia, z)
{
    var en_d = "";
    var ec_s = 0;
    var co_u = varia.length - from.length;

    to = to.replace("@i@", z);
    if(varia.match(from)) // It's in the text
    {
        for(ec_s = 0; ec_s < co_u; ec_s++)
        {
            if(varia.substring(ec_s, (ec_s + from.length)).match(from))
            {
                en_d = varia.substring(ec_s + from.length, varia.length);
                varia = varia.substring(0, ec_s);
// Debug purposes
alert("en_d contains:\n" + en_d);
alert("varia contains:\n" + varia);
                varia = varia + to + en_d;
            };
        };
    };

    return varia;
};


It always shows up an error on line 9 of the code. " ' ) ' is expected "

OK, that solved, but now the function doesn't do anything when I run it. :(

Help me,
purge.exe
Offline Profile Quote Post Goto Top
 
Dennis
Member Avatar
Member
[ *  *  *  *  *  *  * ]
Quote:
 
    if(varia.match(from)) // It's in the text
Why is there two )'s?
Offline Profile Quote Post Goto Top
 
Rory
Member Avatar
i;m a mess
[ *  *  *  *  *  *  * ]
one closes the IF condition, the other closes the paramater of .match
Offline Profile Quote Post Goto Top
 
purge.exe
Member
[ *  *  *  * ]
Line 9
 
        while(ec_s = 0; ec_s < co_u; ec_s++)


This is the line that causes the error.
New error, read first post.

Help is appreciated,
purge.exe
Offline Profile Quote Post Goto Top
 
Sithtradmus
Member
[ * ]
Maybe you should remove the two '))' and see what happens.
Offline Profile Quote Post Goto Top
 
FearKiller
Member Avatar
www.drewscripts.com
[ *  *  *  *  * ]
Sithtradmus
December 30, 2004 05:30 PM
Maybe you should remove the two '))' and see what happens.

I don't know what the problem is, but I know that isn't it.
Offline Profile Quote Post Goto Top
 
purge.exe
Member
[ *  *  *  * ]
I fixed it, but there's a new error. It doesn't do a thing, no matter what is passed to the function.

Help... me...,
purge.exe
Offline Profile Quote Post Goto Top
 
Gogf
Member Avatar
Indescribable
[ *  *  *  *  *  * ]
What exactly is it supposed to do?
Offline Profile Quote Post Goto Top
 
Seth
Member Avatar
I has a pony
[ *  *  *  *  *  *  *  *  * ]
It would help if you used descriptive variable names :ermm:
Offline Profile Quote Post Goto Top
 
purge.exe
Member
[ *  *  *  * ]
Sorry about that. Here's a list of variables and their uses:
from - What you want replaced
to - What you want it replaced with
varia - The string to replace from to to
z - What to replace the string @i@ with (don't worry about this one)
en_d - The stuff after from in varia
ec_s - The loop variable. I use this because (for some reason), varia.indexOf(from) does not work
co_u - Used to stop the loop from going further than it can. from cannot be found in a string that is smaller than it, so that's what this is used for, in other words

Line-by-line walkthrough:
to = to.replace("@i@", z); - Don't worry about this part
if(varia.match(from)) // It's in the text - If it is found, do the calculations
for(ec_s = 0; ec_s < co_u; ec_s++) - Keep looking for from in varia (same as indexOf, although indexOf DOESN'T WORK FOR SOME DAMN REASON)
if(varia.substring(ec_s, (ec_s + from.length)).match(from)) - Have we found from?
en_d = varia.substring(ec_s + from.length, varia.length); - Cut the data after where from was found and put it in en_d
varia = varia.substring(0, ec_s); - Cut out the (first) from in varia
// Debug purposes - Show me was the variables are right now
varia = varia + to + en_d; - Replace the text
break; - I forgot this line of code :$ Don't go any further, as we only want to replace the first one
return varia; - Return the replacement (if there was one)


Is that enough for yeh?,
purge.exe
Offline Profile Quote Post Goto Top
 
SilverSky
Member
[ * ]
I think I can see what you are trying to do* and you don't need to create a function.

If you use the string replace method combined with a regular expression with a global flag then it should accomplish what you are trying to do...

Examples:

  • myVar.replace(/Rong/g, "Wrong")
  • myVar.replace(new RegExp("Rong", "g"), "Wrong")
*You are trying to replace every match of a substring within a string?
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
ZetaBoards - Free Forum Hosting
Create your own social network with a free forum.
« Previous Topic · Technology Chat · Next Topic »
Add Reply