Is it possible to partially replace the url of the image img ? It is necessary that the url is changed to the given one and only up to definitely words, in this case it is newwiki .

This code finds images and changes the url, replacing /newwiki/ with http://drug/newwiki . At the same time, the initial path obviously does not change and the url remains incorrect.

 var img = $('img').first(); $.each(img.get(0).attributes, function() { console.log(img); if (this.name != 'data-base-url') { img.attr(this.name, img.attr(this.name).replace(/newwiki/, 'http://drug/newwiki')); } }); 

The main difficulty is that the first part of the url can be different, and not known in advance.

How can I change the url in this case?

UPD

An example of a valid and incorrect url:

Invalid url http://crm.mv.portal-new.web-dev.ru/newwiki/s/ru_RU/6217/82a642d727bee456b0a91262638763d5620b1a86.8/_/images/icons/emoticons/smile.png

right url http://drug/newwiki/s/ru_RU/6217/82a642d727bee456b0a91262638763d5620b1a86.8/_/images/icons/emoticons/smile.png

  • one
    It is not quite clear what you want to do, but I will try to guess. Try changing it like this: replace (/ newwiki /, '/ drug / newwiki')) - Xander
  • In general, please provide some example of how you can have the original URL, and what you would like to get from it after the replacement. - Xander
  • Thank you very much. It works for me) it remains only to test for those who did not work. - Marina Voronova
  • Marina, then I will now take out my comment in response. And you, when you make sure that everything works, mark this answer as a solution. Or if something goes wrong - write a comment. - Xander

1 answer 1

Try to change it like this:

 replace(/newwiki/, '/drug/newwiki'))