I have image URLs like this
https://cdn.shopify.com/s/files/1/0919/5078/products/DSC_9782-processed_9bef16cb-7700-48d9-ad6d-f9a350e7f6c7.jpg?v=1579953393
I need to insert _small
before the file extension in order to get that image size and it should look like this
https://cdn.shopify.com/s/files/1/0919/5078/products/DSC_9782-processed_9bef16cb-7700-48d9-ad6d-f9a350e7f6c7_small.jpg?v=1579953393
(Images can have different file extensions.)
What’s the best way to do this with JavaScript? Maybe Regex?
Tried using this Regex, but it doesn’t work with the ?v=1579953393
after the file extension.
6
Answers
I did manage to rework this answer to do the job. It looks like this and it works as expected.
One approach can be this, where you are sure that image extension is .jpg.
Then you can
split
the string.Evening using
URL
interface you can achieve this.This should help you. You just need to add each extension type that could be used to the
fileTypes
array.Hope , it will help you.
You can use this Regex:
Then replace groups like:
My solutions aren’t always the shortest or the most elegant, however, I think they are their weight in readability.
I didn’t like the extensibility or readability of the solutions above so I wrote this function: