Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 16th, 2004, 10:39 AM
Registered User
 
Join Date: Jan 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default replace()

Hi, I am really struggling with a solution for a replacment in a string:

I am tring to replace the IMG tag within a sting containing a whole bunch of html code.

All my functions calls work fine. What I am left with is a different IMG tag depending on weather the image is clickable or not.

function getImageBio(htmlIMG)
{
    var startHREF = "";
    var endHREF = "";
    var tempIMG = "";
    var bioView = "";

    startHREF = htmlIMG.indexOf("<a href=\"bios");
    endHREF = htmlIMG.indexOf("Biography'><\a>");
     tempIMG = htmlIMG.substring(startHREF.endHREF);

    var bioFile = getBio(htmlIMG);
    var picFile = getPic(htmlIMG);

    if(bioFile == "bios/.")
        bioView = viewNoBio(picFile);
    else
         bioView = viewBio(picFile, bioFile);

    htmlIMG.replace(tempIMG, bioView);

    return(bioView);
}

The red font is where I am getting lost. I need a way to replace the tempIMG string with in the htmlIMG string with the bioView string.

Hope this isn't too confusing. I just can't figure it out. Can anyone help???

Reply With Quote
  #2 (permalink)  
Old January 16th, 2004, 11:17 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

It might help if I could see the data passed as htmlIMG but I think you need:
Code:
var oRE = new RegExp(tempImg, "i");
htmlIMG.replace(oRE, bioView)
String.protoype.replace needs a regular expression as its first argument and a string as its second. It also doesn't replace inline, it trerns a new String with the new text. Regular expressions can be created quickly using the forward slash delimiters, /, but these take a string literal. You are using a variable, tempIMG so you need the two stage creation shown above. The i flag indicates case insensitive. The main problem is that if your target string contains special characters, especially back slashes, \, you need to double these up before creating the Regular Expression.
As you know where the string starts and ends it might be easier just using String methods. Concatenate the string from htmlIMG upto the start of the a tag, the new text (bioView), and add on the text after the end of the a tag.
If you show me an example of possible inputs and desired outputs perhaps I can come up with a better explanation.



--

Joe
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace In c# jayanth_nadig SQL Language 2 July 3rd, 2006 03:25 AM
replace ' and " with \' and \" Brian Campbell XSLT 4 May 24th, 2006 10:49 AM
replace "." with "/" thelos Excel VBA 1 September 14th, 2005 10:47 AM
Replace crmpicco VB How-To 15 May 20th, 2005 01:34 PM



All times are GMT -4. The time now is 10:05 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.