1

I have used this code to get the img src attribute:

$('.listpic').attr('currentSrc');

The image url is: newyorkmodelhstn.jpg. I have to remove tn from this to get the original img src. How can I do this?

Srikanth Naidu
  • 787
  • 5
  • 16
  • 28

3 Answers3

1

first rename the string then access it:

var str="newyorkmodelhstn.jpg";
var find = 'tn.';
var re = new RegExp(find, 'g');
str = str.replace(re, '.');//use this to get the image
alert(str);

or you can do this:

var str="newyorkmodelhstn.jpg";
str = str.replace("tn.", '.');//use this to get the image

visit this thread for good explanation and understanding:here

Community
  • 1
  • 1
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
  • why the use of RegExp ? The only use case would be to get all occurences of `"tn."` which is obviously not what OP does want. – Kaiido Oct 05 '15 at 08:58
  • @Kaiido it can be done with normal replace also since he needs only one replacement. – Suchit kumar Oct 05 '15 at 09:00
1

It is simple, i add dot because a 'th' can be in a middle word

var str = 'newyorkmodelhstn.jpg';
var result = str.replace('tn.', '.');
Artem Chernov
  • 856
  • 2
  • 8
  • 26
1

You can try this..

var imgs=document.getElementsByClassName("listpic"); 


for(var i=0;i< imgs.length;i++) 
{
var str=imgs[i].src;
var find='tn.';
var re=new RegExp(find,'g');
str=str.replace(re, '.');
var a = $("<a>").attr("href", str).attr("download", imgs[i].alt).appendTo("body");
a[0].click();
a.remove();  }
jitendra rathore
  • 236
  • 1
  • 2
  • 7