want to alter one character in an image src

Discussion in 'JavaScript' started by somebody, Jun 5, 2005.

  1. #1
    how do i take a .src (document.images[0].src =, for instance) and change one character in it? i'm working with an array which contains filenames, either 8 or 9 characters in length (keyword+#val+sub-val+".jpg") i want to change the character in the sub-val. ex: pict1b.jpg - change to pict1c.jpg, -or-, img3b.jpg - change to img3c.jpg...

    change that "b" to a "c"

    there will be no other instances of the letter "b" or "c" in these filenames;

    how do i take the .src and use indexof and replace to make the b into a c?

    i'm not quite intermediate, yet, so go easy on me! heeh

    =b
     
    somebody, Jun 5, 2005 IP
  2. NewComputer

    NewComputer Well-Known Member

    Messages:
    2,021
    Likes Received:
    68
    Best Answers:
    0
    Trophy Points:
    188
    #2
    Is all the text contained withing one document? If yes, use notepad 'control+h'... I think this is what you are asking...
     
    NewComputer, Jun 5, 2005 IP
  3. somebody

    somebody Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    er,

    user clicks an image in webpage, new window opens, displays that image; how can i take that image's src and change one character in it? i would like to know the javascript to include to take document.images[0].src and subtract the "b" and replace it with a "c", the "b" will be the 5th or 6th character in every src, and there will be no other uses of "b" or "c" in the filenames.

    i have pict1a.jpg, pict1b.jpg, pict1c.jpg for each picture, "a" is thumbnail, "b" is slideshow version, "c" is the fullsize one; i don't want to make a new array, i want to change the "b" to a "c" in the filename.
     
    somebody, Jun 5, 2005 IP
  4. NewComputer

    NewComputer Well-Known Member

    Messages:
    2,021
    Likes Received:
    68
    Best Answers:
    0
    Trophy Points:
    188
    #4
    Yea, that sounds like J.D. territory. I am sure if JD is around, he can chime...
     
    NewComputer, Jun 5, 2005 IP
  5. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Once you know the index of the character you want to replace, use one of the substring functions (substr, substring or slice) to recompose the string out of three parts - the part before the character, the alternative character and the part after:

    var s = "abc5def";
    var i = s.indexOf("5");
    new String().concat(s.substring(0,i), "0", s.substring(i+1,s.length));

    If you want to go fancy style, you can use regex. For example, the following code will replace numbers in the middle of the string with a dot:

    "abc123def".replace(new RegExp("([a-z]+)[0-9]+([a-z]+)"), f);

    function f(p1, p2, p3, p4, p5)
    {
    return p2 + "." + p3;
    }

    This won't work in older browsers, though - this form of replace wasn't in the first JS versions.

    J.D.
     
    J.D., Jun 6, 2005 IP
  6. somebody

    somebody Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you da man, i thought i was on the right track, well, i was/am, here!

    so it's indexof, substr, and concat... thanks greatly!
     
    somebody, Jun 6, 2005 IP