0

I want to replace a specific part of a url, but only within a specific div, been trying with regex (yes I'm a regex newbie) but just cant wrap my head around how to get this to work and would appriciate any helping hand

I want to replace /images/ with a new folder /design/abtester/nyaproduktbilder/ Reason for this is that I want to run a sitewide A/B test with new product images

I have this part of code

<div class="visaprodbild"><a class="fancyBoxLink" rel="produktbilder" href="/images/BR501.jpg"><img class="hand" alt="" title="" src="/images/BR501.jpg" border="0" width="330px"></a></div>

Problem is that I cant just find and replace the /images/ part, cause there is a lot more other images on the page using /images/ and I cant move the original product images to a new folder.

So I need to identify the /images/ part within the div above

Sorry if this was an unclear question for help, but just so lost I can hardly explain what I need to do.

Per
  • 97
  • 1
  • 1
  • 8

2 Answers2

0

I don't think you even need regex.. you can just do .replace('/images/','yournewpath')

$('div.visaprodbild img.hand').attr('src',function(i,v){
    return v.replace('/images/','/your new path/');
});

By using attr('src',function(i,v) the old value gets passed in as an argument and you can return the modified inside the function

FIDDLE

wirey00
  • 33,517
  • 7
  • 54
  • 65
  • wow, worked like a charm :D <3 You just saved my day. Thx m8 For some reason original image names sometimes contains () in the name, like BR501(1).jpg. Is it possible to also clean that up IF the image url contains ()? – Per Mar 11 '13 at 22:32
  • you just want to remove any and all `(` and `)`? – wirey00 Mar 11 '13 at 23:34
-1

Add an ID to <a>and <img> and change it's attributes with $("#id").attr();

Mollo
  • 723
  • 3
  • 7
  • 24
  • Why the downvote ? Maybe is not the best answer but, it's a solution to his problem. – Mollo Mar 11 '13 at 22:04
  • Good idea, thx alot for your help. Problem I have is that I cant add an ID cause I dont have any control over the code as its a co-hosted central solution – Per Mar 12 '13 at 21:18