Discussion:
Replacing a portion of an href value
Randall Morgan
2010-02-02 00:12:04 UTC
Permalink
Hello,

I have the following code which is part of a loop to add preview
images to a filmstrip where each cell is made up pf a div. My
filmstrip uses a tiny b&w version of the image which is stored in the
images/s/ folder. My preview images must come from the images/m/
folder. I have tried using replace() but have not been successful at
it. Any help would be greatly appreciated.

Code:

$("body").append("<div id='preview'><img src='"+ this.href +"'
alt='Image preview' />"+ c +"</div>");

Original href's are similar to: <sitename>.com/images/s/<image_name>.png

I need the following replacement: <sitename>.com/images/m/<image_name>.jpg

So I need to replace the sub-folders and the file extensions within
the line above.


Thanks,

Randy
--
If you ask me if it can be done. The answer is YES, it can always be
done. The correct questions however are... What will it cost, and how
long will it take?
Andreas Möller
2010-02-02 01:13:01 UTC
Permalink
I don't know about regular expressions in Javascript, but actually you just want to match the name of the image and prepend it with a different path and append a different suffix to, so something like

preg_match('/^\com\/images\/s\/([a-z0-9_])+\.png$/', $href, $match);
$href = 'com/images/s/' . $match[1] . '.png';

- only in Javascript.


Best regards,

Andreas
Randall Morgan
2010-02-02 01:31:12 UTC
Permalink
Hi,

actually my image names (35 of them) as coming from an ajax call and I
loop through the results assigning the images to the filmstrip cells.
Then I setup the mouse overs for the popup preview. If this were php
or C I'd have not trouble. But with Javascript and JQuery I'm lost...
Post by Andreas Möller
I don't know about regular expressions in Javascript, but actually you just want to match the name of the image and prepend it with a different path and append a different suffix to, so something like
preg_match('/^\com\/images\/s\/([a-z0-9_])+\.png$/', $href, $match);
$href = 'com/images/s/' . $match[1] . '.png';
- only in Javascript.
Best regards,
Andreas
--
If you ask me if it can be done. The answer is YES, it can always be
done. The correct questions however are... What will it cost, and how
long will it take?
Randall Morgan
2010-02-02 01:42:29 UTC
Permalink
Hi All,

I figured it out. I was making it far more complex than it needed to be ;-)

this.href.replace('/s/', '/m/')

Thanks,
Post by Randall Morgan
Hi,
actually my image names (35 of them) as coming from an ajax call and I
loop through the results assigning the images to the filmstrip cells.
Then I setup the mouse overs for the popup preview. If this were php
or C I'd have not trouble. But with Javascript and JQuery I'm lost...
Post by Andreas Möller
I don't know about regular expressions in Javascript, but actually you just want to match the name of the image and prepend it with a different path and append a different suffix to, so something like
preg_match('/^\com\/images\/s\/([a-z0-9_])+\.png$/', $href, $match);
$href = 'com/images/s/' . $match[1] . '.png';
- only in Javascript.
Best regards,
Andreas
--
If you ask me if it can be done. The answer is YES, it can always be
done. The correct questions however are... What will it cost, and how
long will it take?
--
If you ask me if it can be done. The answer is YES, it can always be
done. The correct questions however are... What will it cost, and how
long will it take?
Continue reading on narkive:
Loading...