PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, June 30, 2022

[FIXED] How can I insert text before the file extension with JavaScript?

 June 30, 2022     javascript, shopify     No comments   

Issue

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.


Solution

I did manage to rework this answer to do the job. It looks like this and it works as expected.

image = 'https://cdn.shopify.com/s/files/1/0919/5078/products/DSC_9782-processed_9bef16cb-7700-48d9-ad6d-f9a350e7f6c7.jpg?v=1579953393';
image = image.replace(/(\.[\w\d?=_-]+)$/i, '_small$1');


Answered By - Georgi Demirev
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing