Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Replace() Method Help
Old 08-11-2009, 02:54 PM Replace() Method Help
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Hi there,

I'm trying to make something that changes all 'hello's to 'goodbye's, and all 'dog's to 'cat's. Here's what I've got.

Code:
<script type="text/javascript">

var str="Hello there cat, and hi other dog. ";
document.write(str.replace(/Hello/g, "Goodbye"),(/dog/g, "cat"));


</script>
I want that above code to output "Goodbye there cat, and hi other cat.", but it outputs "Goodbye there cat, and hi other dog. cat"

Also this:

<script type="text/javascript">

var str="Hello there cat, and hi other dog. Hello also other dog!";
document.write(str.replace(/Hello/g, "Goodbye"),(/dog/g, "cat"));


</script>

Which should output "Goodbye there cat, and hi other cat. Goodbye also other cat!"

But it outputs "Goodbye there cat, and hi other dog. Goodbye also other dog!cat"

WHY DOES THIS NOT WORK?? I'm obviously not making it replace useless things like 'god' and 'cat', but other things. I made it simple so it's easier to understand.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-11-2009, 05:39 PM Re: Replace() Method Help
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Because you cannot chain several changes with replace(replace1, replace2)
Beside, look closely to your code, the "(/dog/g, "cat")" is a second parameter to document.write(), not str.replace()

document.write(str.replace(/Hello/g, "Goodbye"),(/dog/g, "cat"));

You have to work in 2 phases
Code:
var str="Hello there cat, and hi other dog. ";
str=str.replace(/Hello/g, "Goodbye");
str=str.replace(/dog/g, "cat");
document.write(str);
or, smaller
Code:
var str="Hello there cat, and hi other dog. ";
str=str.replace(/dog/g,str.replace(/Hello/g, "Goodbye"));
document.write(str);
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-11-2009, 08:28 PM Re: Replace() Method Help
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
Quote:
Originally Posted by tripy View Post
Because you cannot chain several changes with replace(replace1, replace2)
Beside, look closely to your code, the "(/dog/g, "cat")" is a second parameter to document.write(), not str.replace()

document.write(str.replace(/Hello/g, "Goodbye"),(/dog/g, "cat"));

You have to work in 2 phases
Code:
var str="Hello there cat, and hi other dog. ";
str=str.replace(/Hello/g, "Goodbye");
str=str.replace(/dog/g, "cat");
document.write(str);
or, smaller
Code:
var str="Hello there cat, and hi other dog. ";
str=str.replace(/dog/g,str.replace(/Hello/g, "Goodbye"));
document.write(str);
THANK YOU! The first one works like a charm, but the second one doesn't work... But I'm using the first one, thank you!

If you would like I can give you credit for this in the credits for the webapp I'm working on.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-11-2009, 08:55 PM Re: Replace() Method Help
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
OK, That works, and I've done something with it, but here's what I have now in my entire page so far:

Code:
<html>
<body>
<?php echo"
<script type='text/javascript'>

var str='$userinput';
str=str.replace(/12345/gi, '67890'); //Replaces '12345' with '67890'// 
str=str.replace(/if/gi, 'if (');
str=str.replace(/less than/gi, '<');
str=str.replace(/write/gi, 'document.write(\"');

function outputcode()
{
document.write(str);
}

</script>


<INPUT TYPE=\"text\" SIZE='25' NAME=\"site\" value=\"$userinput\" />
<input type=\"button\" name=\"submitbutton\" value=\"Submit\" onclick=\"outputcode()\"/>

";?>
</body>
</html>
Why doesn't this work? I have a feeling I'm missing something HUGE here but I'm too stupid to see it. Once you mix JS with PHP I just get messed up...

BTW sorry for double-post :O

And for the example above, try just typing in the box '12345'. Looking at the code, it should replace '12345' with '67890' but nothing happens...
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 08-11-2009 at 08:57 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Replace() Method Help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19699 seconds with 12 queries