I’m writing a code in Javascript where I wanted to replace all the query strings in a string of comma separated urls. But I’m able to do it only till some extent. Below is my code.
const urls = '//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-2.psd?fmt=png-alpha&scl=1&hei=77&wid=92&qlt=100,1&resMode=sharp2&size=92,77&chrss=full,//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-4.psd?fmt=png-alpha&scl=1&hei=59&wid=92&qlt=100,1&resMode=sharp2&size=92,59&chrss=full,//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-6.psd?fmt=png-alpha&scl=1&hei=79&wid=92&qlt=100,1&resMode=sharp2&size=92,79&chrss=full,//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-7.psd?fmt=png-alpha&scl=1&hei=79&wid=92&qlt=100,1&resMode=sharp2&size=92,79&chrss=full,//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-8.psd?fmt=png-alpha&scl=1&hei=59&wid=92&qlt=100,1&resMode=sharp2&size=92,59&chrss=full,//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-9.psd?fmt=png-alpha&scl=1&hei=47&wid=92&qlt=100,1&resMode=sharp2&size=92,47&chrss=full,https://i.dell.com/das/dih.ashx/57x57/sites/csimages/Video_Imagery/all/lati-ys-video-thumbnail-hero-1280x720.jpg';
const urlsWithoutQueryParams = urls.replace(/(?.*?(?=,)|?.+$)/g, '');
console.log(urlsWithoutQueryParams);
And my Expected output is
//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-2.psd
//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-4.psd
//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-6.psd
//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-7.psd
//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-8.psd
//i.dell.com/is/image/DellContent/content/dam/ss2/products/laptops-and-2-in-1s/latitude/13-9330-touch/media-gallery/2in1/notebook-latitude-13-9330-gray-gallery-9.psd
https://i.dell.com/das/dih.ashx/57×57/sites/csimages/Video_Imagery/all/lati-ys-video-thumbnail-hero-1280×720.jpg
Please lemme know where I’m going wrong.
Thanks
2
Answers
As all the URLs also contains
,
, there’s no quick way to solve this using Regex (There’s probably a solution in Regex, but sometimes Regex is not the only solution). Here’s an alternate solution for your particular case:I hope this helps you!
Regular way
If you needed more elaboration just comment!
Just regex solution:
This solution uses just one line of code.