0

I know the question i've posted here is quit silly but after hours of googling and trying all most everything i could not get the desired result. So let me share it here in a hope to get solved here.

My problem is, i've an Image url source where i need to change Image name with new image name everytime. I'll post my tried code:

$(function () {
     //Old Image Url where i need to change 32_Penguins.jpg with new file name
     var ImageSrcUrl="../Upload\UserProfile\32_Penguins.jpg"; //Don't change forward slash with backword slash here and then try to answer
     var NewFileName="Tullips.jpg";

     //I tried to replace everything after the last "\"[slash]
     ImageSrcUrl= ImageSrcUrl.replace(/.*$/i, NewFileName); 

     alert(ImageSrcUrl);
});

Note: Please don't try to replace forward slash with backward slash manually and then answer it[Bcoz it works but conversion should be done via jquery code not manually]

2
  • 1
    images path contains backword or forword slash? Commented May 20, 2016 at 7:25
  • I double checked my code and found that problem was in my Image tag where i was giving src attribute. Sorry everyone for giving burden, and thanks everyone for solving it. Commented May 20, 2016 at 9:03

5 Answers 5

1

$(function() {
      //Old Image Url where i need to change 32_Penguins.jpg with new file name
      var ImageSrcUrl = "../Upload\\UserProfile\\32_Penguins.jpg";
      var index = ImageSrcUrl.lastIndexOf("\\") + 1;
     var path = ImageSrcUrl.substr(0,index);
     var NewFileName="Tullips.jpg ";
     ImageSrcUrl= path+NewFileName; 
     alert(ImageSrcUrl);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

try to change to lastindexof("\\") @Amar and write ImageSrcUrl escaping `` as well.
1
$(function(){
     var ImageSrcUrl = "../Upload/UserProfile/32_Penguins.jpg";

     var NewFileName = "Tullips.jpg";
     var index = ImageSrcUrl.lastIndexOf("/") + 1;
     var filename = ImageSrcUrl.substr(index);
     ImageSrcUrl = ImageSrcUrl.replace(filename, NewFileName); 
     alert(ImageSrcUrl);
});

Comments

0

$(function () {
     //Old Image Url where i need to change 32_Penguins.jpg with new file name
     var ImageSrcUrl="../Upload/UserProfile/32_Penguins.jpg";
     var NewFileName="Tullips.jpg";

     var parts = ImageSrcUrl.split('/');

     parts[parts.length -1] = NewFileName;
     alert(parts.join('/'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0
**I wrote an example for this one:**

$(function () {
    var str = "ss/ll/Test.jpg";
      var m = /[^/]*$/.exec(str)[0];
      var sts = str.replace (m, "Tst.jpg");
      display("Input: " + str);
      display("Replaced: " + sts);

      function display(msg) {
        var p = document.createElement('p');
        p.innerHTML = msg;
        document.body.appendChild(p);
      }
    });

Comments

0

I modified your initial function:

function aa() {
    var ImageSrcUrl="../Upload/UserProfile/32_Penguins.jpg";
    var NewFileName="Tullips.jpg";
    ImageSrcUrl = ImageSrcUrl.replace(/[A-z0-9.]*$/i, NewFileName); 
    return ImageSrcUrl;
};

From what I tested it should work.

1 Comment

I don't understand your comment. I did change the `\` because AFAIK urls don't have them.

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.