简体   繁体   English

用于验证图像 url 的正则表达式,它可能包含 jpeg、jpg、gif、png 等扩展名

[英]Regex to validate image url which may contains extension like jpeg, jpg, gif, png

Regex to validate image url which may contains extension like jpeg, jpg, gif, png用于验证图像 url 的正则表达式,它可能包含 jpeg、jpg、gif、png 等扩展名

I'm using我正在使用

/\.(jpg|jpeg|png|gif|webp)(\?.*)*$/i

but it gives false for https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/macbook-air-gallery3-20201110?wid=4000&hei=3072&fmt=jpeg&qlt=80&.v=1603399121000但它给出了错误的https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/macbook-air-gallery3-20201110?wid=4000&hei=3072&fmt=jpeg&qlt=80&.v=1603399121000

I'm expecting a regex which can give true if this extensions like jpg, jpeg, png, gif, webp are present in url.如果 url 中存在像 jpg、jpeg、png、gif、webp 这样的扩展,我期待一个可以给出 true 的正则表达式。

I don't know what you'd like to do exactly but this should return true :我不知道你到底想做什么,但这应该返回true

 var text = "https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/macbook-air-gallery3-20201110?wid=4000&hei=3072&fmt=jpeg&qlt=80&.v=1603399121000"; const regex = new RegExp(/(\W)(jpg|jpeg|png|gif|webp)(\W)/); console.log(regex.test(text));

You can use this regex to test for URL parameter fmt=<format> :您可以使用此正则表达式来测试 URL 参数fmt=<format>

  • first example: fmt=jpeg in middle第一个例子: fmt=jpeg在中间
  • second example: fmt=jpeg at end第二个例子:最后是fmt=jpeg
  • third example: fmt=bmp (unsupported format)第三个例子: fmt=bmp (不支持的格式)

 const regex = /\bfmt=(jpg|jpeg|png|gif|webp)(?=(&|$))/; [ 'https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/macbook-air-gallery3-20201110?wid=4000&hei=3072&fmt=jpeg&qlt=80&.v=1603399121000', 'https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/macbook-air-gallery3-20201110?wid=4000&hei=3072&qlt=80&.v=1603399121000&fmt=jpeg', 'https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/macbook-air-gallery3-20201110?wid=4000&hei=3072&fmt=bmp&qlt=80&.v=1603399121000', ].forEach(url => { console.log(url + ' => ' + regex.test(url)); });

Output: Output:

https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/macbook-air-gallery3-20201110?wid=4000&hei=3072&fmt=jpeg&qlt=80&.v=1603399121000 => true
https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/macbook-air-gallery3-20201110?wid=4000&hei=3072&qlt=80&.v=1603399121000&fmt=jpeg => true
https://store.storeimages.cdn-apple.com/4668/as-images.apple.com/is/macbook-air-gallery3-20201110?wid=4000&hei=3072&fmt=bmp&qlt=80&.v=1603399121000 => false

Explanation of regex:正则解释:

  • \b -- word boundary \b -- 单词边界
  • fmt= -- literal fmt= parameter (this is to avoid false positives) fmt= -- 文字fmt=参数(这是为了避免误报)
  • (jpg|jpeg|png|gif|webp) -- logical 'or' of supported file extensions (jpg|jpeg|png|gif|webp) -- 支持的文件扩展名的逻辑“或”
  • (?=(&|$)) -- positive lookahead for either & or end of string (?=(&|$)) -- 对&或字符串结尾的正向预测

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何限制除jpg png或gif以外的图像大小和扩展名 - How to limit image size and extension other then jpg png or gif 正则表达式在CKeditor的HTML中查找png-gif-jpg图像链接 - Regex to find png-gif-jpg image links in HTML of CKeditor JS / Jquery,匹配找不到PNG =匹配(&#39;/ gif | jpg | jpeg | png /&#39;) - JS/Jquery, Match not finding the PNG = match('/gif|jpg|jpeg|png/') 在 django 项目中图像扩展是否重要(如 jpg 和 png) - does image extension matters in django projects (like jpg and png) 通过 angular 6 应用程序中的 javascript 更改 base64 图像大小并下载不同扩展名(png、jpg、gif 等) - Change base64 image size and download with different extension(png, jpg, gif, etc…) by javascript in angular 6 application 有没有一种方法不让Blob(createObjectURL)显示GIF文件但扩展名为.jpeg的图像? - Is there a way not to let a blob (createObjectURL) show the image which is a GIF file but has a .jpeg extension? 检查链接(不是URL)是否包含一些文本(.jpg,.gif,.png) - Check if link (not URL) contain some text (.jpg, .gif, .png) 如何在节点 js 中获取图像(JPG、JPEG、PNG)的 EXIF 数据? - How to get EXIF data of an image (JPG, JPEG, PNG) in node js? 将Image jpg,png,jpeg…保存在CloudBoost后端的一列中作为服务 - Save Image jpg, png, jpeg… in a column in CloudBoost backend as a service 仅当字符串文件名具有 .jpg 或 .png 扩展名时才使用正则表达式替换 - Using regex to to replace only if the string filename has .jpg or .png extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM