Justin
Justin

Reputation: 329

How to use JavaScript or jQuery to trim dynamic URL from absolute to relative

The system I'm using occasionally adds the full path to the src of a dynamic img.

<a href="#" class="link">
    <img src="http://www.domain.com/images/wish-add.png" alt="" />
</a>

I need everything before /images to to be removed.

I need be able to do it without manually referencing the domain. The domain is not always going to be the same.

I can remove parts that are always the same.. Like /images.. but I don't know how to remove anything before /images regardless of what it is.

Upvotes: 1

Views: 385

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 219920

$('img').attr('src', function (i, src) {
    return src.replace(/https?:\/\/[^\/]+/, '');
});

Upvotes: 3

Related Questions