0

I'm trying to perform a find/replace on the src attribute of an image tag, to remove part of the filename of the image. I assume I need to use str.replace(), but I'm not sure how to write the regex to accomplish what I'm trying to do.

The src attribute is currently

http://domain.com/path/to/file/D063DC58-6051-4B24-8CDC-D4525F72A150_tn.jpg

where /to/file/xxxxxxx_tn.jpg will vary, with the filename always ending in _tn.jpg. I'd like to remove the _tn from each instance on the page.

2
  • looks like grabbing facebook images, from _q (quick /small thumb) to _n (normal size) ;) Commented Oct 4, 2011 at 14:47
  • jQuery is certainly an option. It's not grabbing facebook images, FWIW. Commented Oct 4, 2011 at 14:49

4 Answers 4

2
var images = document.getElementsByTagName('img');

for (var i = 0; i < images.length; i++) {
    images[i].src = images[i].src.replace('_tn.jpg', '.jpg');
}
Sign up to request clarification or add additional context in comments.

Comments

2
var srcValue = "http://domain.com/path/to/file/D063DC58-6051-4B24-8CDC-D4525F72A150_tn.jpg";

var newSrcValue = srcValue.replace(/[A-Z0-9\-]+_tn/, 'xxxxx_tn');

Comments

0

You don't need to use a regular expression.

referenceToImage.src = referenceToImage.src.replace('_tn', '');

2 Comments

I'll go ahead and show my ignorance and ask: what's the best way to select the image src? My javascript skills are weak at best.
By using the src property as per the example in this answer.
0

if you are using jQuery >= 1.1:

$("img").attr("src", function(i, val) {
    return val.replace("_tn.jpg", ".jpg");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.