javascript 替换查询字符串

j8yoct9x  于 2023-04-28  发布在  Java
关注(0)|答案(3)|浏览(137)

我写了一个代码在Javascript中,我想取代所有的查询字符串在一个字符串逗号分隔的网址。但我只能做到一定程度。下面是我的代码。

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);

我的期望输出是
www.example. com www.example. com www.example. com www.example. com www.example. com www.example. com https://i.dell.com/das/dih.ashx/57x57/sites/csimages/Video_Imagery/all/lati-ys-video-thumbnail-hero-1280x720.jpg
请让我知道我错在哪里。
谢谢

33qvvth1

33qvvth11#

我希望这对你有帮助!

常规方式

如果你需要更多的细节,只是评论!

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
  .split(/,(?=\/\/|https:\/\/)/)
  .map((x) => x.replace(/\?.+$/, ''))
  .join(' ');

console.log(urlsWithoutQueryParams);

Just regex解决方案:

这个解决方案只使用一行代码。

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(/\?[^\/]+,\s*(?=\/\/|https:\/\/|$)/g, ' ');

console.log(urlsWithoutQueryParams);
pod7payv

pod7payv2#

由于所有URL都包含,,因此没有快速的方法可以使用Regex解决这个问题(Regex可能有一个解决方案,但有时Regex并不是唯一的解决方案)。以下是针对您的特定情况的替代解决方案:

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
  .split(/(?:,?\/\/|https:\/\/)/)
  .filter(Boolean)
  .map((x) => x.split('?')[0])
  .join(',');

console.log(urlsWithoutQueryParams);
wz3gfoph

wz3gfoph3#

由于查询参数中有逗号,所以只匹配URL路径部分并将它们重新连接成字符串可能更容易:

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.match(/(?:https:)?\/\/[^?]*/g).join(',')

console.log(urlsWithoutQueryParams)

相关问题