Wrox Programmer Forums
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 16th, 2004, 10:39 AM
Registered User
Join Date: Jan 2004
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);
         bioView = viewBio(picFile, bioFile);

    htmlIMG.replace(tempIMG, 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???

Old January 16th, 2004, 11:17 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

It might help if I could see the data passed as htmlIMG but I think you need:
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.



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

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.