0

I've got a piece of javascript that dynamically calls an image URL

if (searchResult['image'] != undefined) {
                    if (searchResult['link'] != undefined) {
                        searchResultNode += '<a class="image" href="' + searchResult['link'] + '">';
                    }

                    searchResultNode += '<img src="' + searchResult['image'] + '" class="ss360-search-result-image" />';

                    if (searchResult['link'] != undefined) {
                        searchResultNode += '</a>';
                    }

                }

What function can I write to edit the img src URL so that it removes the file name suffix (.png, .jpg, .jpeg), adds '-764x375' to the URL, and then readds the filename suffix?

  • What if the image doesn't have a suffix? – Pekka Mar 11 '17 at 14:37
  • 1
    If you can always rely on there being a suffix, you could replace every occurrence of e.g. `.jpg` by `-764x375.jpg` if it occurs at the end of the string. See e.g. http://stackoverflow.com/questions/2729666/javascript-replace-last-occurence-of-text-in-a-string – Pekka Mar 11 '17 at 14:38
  • You can split the string at the last index of . to getan array and you know what to do next. – CaptainHere Mar 11 '17 at 14:42

0 Answers0