JS replace help

This is a discussion on "JS replace help" within the JavaScript Forum section. This forum, and the thread "JS replace help are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Dec 5th, 2007, 04:44
New Member
Join Date: Dec 2007
Location: India
Age: 28
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
JS replace help

Hello friends,

I want to use replace function in javascript but the client wants that a particular word should be replaced a specified number of times.

For example...
In a particular page, I want to replace first 5 occurances of the word 'Telecom' to 'Telecommunications'. No matter how many times this word is used in the specified page.

Please help
Reply With Quote

  #2 (permalink)  
Old Dec 5th, 2007, 18:07
TheSealPortalTeam's Avatar
User of Users

SuperMember
Join Date: May 2007
Location: Buffalo
Posts: 290
Blog Entries: 15
Thanks: 6
Thanked 1 Time in 1 Post
Re: JS replace help

Ah? server-side is best for that kind-of stuff. Don't force javascript to do a recursive look though an unlimited amount of controls. Unless you have one specific control, this is not a good idea!

Here's a good script.
Code: Select all
<script type="text/javascript">
var visitorName = "TheSealPortalTeam";
var myOldString = "Hello username! I hope you enjoy your stay, username.";
var myNewString = myOldString.replace(/username/g, visitorName);

document.write("Old string =  " + myOldString); 
document.write("<br />New string = " + myNewString);

</script>
Or Just replace [MyTextBox] with the textbox or control

Code: Select all
<script type="text/javascript">
var replaceName= "Telecommunications";
var myOldString = [MyTextBox].value;
var myNewString = myOldString.replace(/telecom/g, replaceName);
[MyTextBox].value = myNewString
</script>
Hope that helps!
Added /Telecom/g instead of "telecom", so you replace ALL of the word.
Last Blog Entry: Planning Ahead (Yesterday)

Last edited by TheSealPortalTeam; Dec 5th, 2007 at 18:18. Reason: I added regular expressions.
Reply With Quote
  #3 (permalink)  
Old Dec 6th, 2007, 01:59
New Member
Join Date: Dec 2007
Location: India
Age: 28
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Re: JS replace help

Quote:
Originally Posted by TheSealPortalTeam View Post
Ah? server-side is best for that kind-of stuff. Don't force javascript to do a recursive look though an unlimited amount of controls. Unless you have one specific control, this is not a good idea!

Here's a good script.
Code: Select all
<script type="text/javascript">
var visitorName = "TheSealPortalTeam";
var myOldString = "Hello username! I hope you enjoy your stay, username.";
var myNewString = myOldString.replace(/username/g, visitorName);
 
document.write("Old string =  " + myOldString); 
document.write("<br />New string = " + myNewString);
 
</script>
Or Just replace [MyTextBox] with the textbox or control

Code: Select all
<script type="text/javascript">
var replaceName= "Telecommunications";
var myOldString = [MyTextBox].value;
var myNewString = myOldString.replace(/telecom/g, replaceName);
[MyTextBox].value = myNewString
</script>
Hope that helps!
Added /Telecom/g instead of "telecom", so you replace ALL of the word.

thanks friend... but, I need that only... replace a speficific number of occurrances... please help
Reply With Quote
  #4 (permalink)  
Old Dec 6th, 2007, 02:52
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: JS replace help

I think it would be better to have this guy change the text himself

You can acheive this in a fairly uneconomical way using

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

// get the text in the page
var bodytext = document.getElementsByTagName('body')[0].innerHTML;

// now loop through 5 times

for (i=0; i< 5; i ++)
{
       // replace the word (with matched word to preserve case
       bodytext = bodytext.replace(/[\s]{1}(telecom)[\s]{1}/i, " $1munications ");
}
// write the new body
document.getElementsByTagName('body')[0].innerHTML = bodytext;
</script>
Place that just before the </body> tag in your document.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #5 (permalink)  
Old Dec 6th, 2007, 03:27
New Member
Join Date: Dec 2007
Location: India
Age: 28
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Re: JS replace help

Quote:
Originally Posted by Rakuli View Post
I think it would be better to have this guy change the text himself

You can acheive this in a fairly uneconomical way using

Code: Select all
 
<script type="text/javascript">
 
// get the text in the page
var bodytext = document.getElementsByTagName('body')[0].innerHTML;
 
// now loop through 5 times
 
for (i=0; i< 5; i ++)
{
       // replace the word (with matched word to preserve case
       bodytext = bodytext.replace(/[\s]{1}(telecom)[\s]{1}/i, " $1munications ");
}
// write the new body
document.getElementsByTagName('body')[0].innerHTML = bodytext;
</script>
Place that just before the </body> tag in your document.

Thanks a lot dear... it works great... but it is not replacing the very first instance of the word business... it is replacing after that...

Please, I am not very much familiar with regex... thats why...

thanks again...
Reply With Quote
  #6 (permalink)  
Old Dec 6th, 2007, 03:36
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: JS replace help

Update the the regex line with

Code: Select all
bodytext = bodytext.replace(/[\s]?(telecom)[\s]{1}/i, " $1munications ");
Previous there had to be a space before it matched, now the space is optional
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #7 (permalink)  
Old Dec 6th, 2007, 04:04
New Member
Join Date: Dec 2007
Location: India
Age: 28
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Re: JS replace help

Thanks a lot friend... it works now...

really appreciate your help...
Reply With Quote
  #8 (permalink)  
Old Dec 6th, 2007, 04:21
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: JS replace help

No problems.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #9 (permalink)  
Old Dec 6th, 2007, 04:44
New Member
Join Date: Dec 2007
Location: India
Age: 28
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [SOLVED] JS replace help

hi Rakuli...

1 more favour please

bodytext = bodytext.replace(/[\s]?(business)[\s]{1}/i, " <a href='a'>business</a> ");

here I want to use a variable instead of the word business both the sides... say the variable is

var keyword2Replace = "business";

How will we do that???

Thanks in advance...
Reply With Quote
  #10 (permalink)  
Old Dec 6th, 2007, 06:16
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: [SOLVED] JS replace help

HTML: Select all
 var keyword2Replace = "business";
bodytext = bodytext.replace("/[\s]?("+keyword2Replace+")[\s]{1}/i", " <a href='a'>"+keyword2Replace+"</a> ");
I think this should work. Try it out....
Reply With Quote
  #11 (permalink)  
Old Dec 6th, 2007, 12:56
TheSealPortalTeam's Avatar
User of Users

SuperMember
Join Date: May 2007
Location: Buffalo
Posts: 290
Blog Entries: 15
Thanks: 6
Thanked 1 Time in 1 Post
Re: [SOLVED] JS replace help

Well glad to see it's solved!
Last Blog Entry: Planning Ahead (Yesterday)
Reply With Quote
  #12 (permalink)  
Old Dec 7th, 2007, 04:07
New Member
Join Date: Dec 2007
Location: India
Age: 28
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [SOLVED] JS replace help

Quote:
Originally Posted by TheSealPortalTeam View Post
Well glad to see it's solved!

It's not working after I changed the code you suggested... big problem for me...

variables in regular expression...
Reply With Quote
  #13 (permalink)  
Old Dec 7th, 2007, 04:36
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [SOLVED] JS replace help

You have you to set it up a bit differently to use a variable.

Code: Select all
var replaceWord = 'business';
var regexObj = new RegExp ("(/[\s]?(" +replaceWord + ")[\s]{1}/", "i");

bodytext = bodytext.replace(regexObj, "<a href='a'>" + replaceWord + "</a> ");
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #14 (permalink)  
Old Dec 7th, 2007, 04:45
New Member
Join Date: Dec 2007
Location: India
Age: 28
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Re: JS replace help

Please check Rakuli...

http://www.ukloancentral.co.uk/con_adds/check/test.htm

It's still not working...
Reply With Quote
  #15 (permalink)  
Old Dec 7th, 2007, 05:56
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: JS replace help

Put this

Code: Select all
var replaceWord = 'business';
var regexObj = new RegExp ("(/[\s]?(" +replaceWord + ")[\s]{1})", "i");
Before the start of the for loop
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #16 (permalink)  
Old Dec 7th, 2007, 06:49
New Member
Join Date: Dec 2007
Location: India
Age: 28
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Re: JS replace help

Hi Rakuli,

I have done this... but it is still not working... this time I have removed the prompt and specified to replace 5 occurances...

http://www.ukloancentral.co.uk/con_adds/check/test.htm

Please see this...
Reply With Quote
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace <iframe> for PHP delusion PHP Forum 2 Jun 20th, 2007 14:51
Using RQ Search and Replace for HTML tag editing diakin Scripts and Online Services 0 Feb 10th, 2007 09:13
Text replace in a table cell on same page gribble Web Page Design 10 Sep 28th, 2005 05:32


All times are GMT. The time now is 10:51.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43