trobbins26
trobbins26

Reputation: 219

Replace part of img path with regex in jquery

I want to look through the entire page on a site and replace relative img paths with "/". For example:

    $("body img").attr("src",function(){
        return this.src.replace("uploads","/uploads");
    });

However, in cases where the relative path could be different than whats above, I want to use regex to account for all the different scenarios. I know the images will all reside in the same location, but the various paths to replace could be "uploads, ../uploads, or ../../uploads, etc.

Any help would be appreciated..?

Upvotes: 0

Views: 814

Answers (1)

Ashirvad
Ashirvad

Reputation: 2377

use this

$('img').each(function(){
    $(this).attr('src',$(this).attr('src').replace(/^/,"/"));
      $('body').append('<br />');
$('body').append($(this).attr('src'));

})​

here is fiddle

http://jsfiddle.net/7yNg7/

Upvotes: 1

Related Questions