正则表达式检查有效的URL是否以.jpg,.png或.gif结尾 [英] Regex to check if valid URL that ends in .jpg, .png, or .gif
问题描述
我希望用户提交有效但也是图片的网址,以.jpg,.png或.gif结尾。
(?:([^:/?#] +):)?(?://([?^ /#] *))([^#?] * \\? \\。(?:jpg | gif | png))(?:\?([^#] *))?(?:#(。*))?
更传统的正则表达式可能如下所示。
^ https?://(?:[a-z0-9 \ - ] + \。)+ [az] {2,6}(?:/ [^ /#?] +)+ \。(?:jpg | gif | png)$
| --- ----- domain ----------- | --- path --- | - extension --- |
编辑请参阅我的其他评论,虽然没有完全回答这个问题,但我认为在这种情况下它可能更有用。但是,我将这里留给 karma-whoring 完整性原因。
I would like users to submit a URL that is valid but also is an image, ending with .jpg, .png, or .gif.
(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\.(?:jpg|gif|png))(?:\?([^#]*))?(?:#(.*))?
That's a (slightly modified) version of the official URI parsing regexp from RFC 2396. It allows for #fragments
and ?querystrings
to appear after the filename, which may or may not be what you want. It also matches any valid domain, including localhost
, which again might not be what you want, but it could be modified.
A more traditional regexp for this might look like the below.
^https?://(?:[a-z0-9\-]+\.)+[a-z]{2,6}(?:/[^/#?]+)+\.(?:jpg|gif|png)$ |-------- domain -----------|--- path ---|-- extension ---|
EDIT See my other comment, which although isn't answering the question as completely as this one, I feel it's probably a more useful in this case. However, I'm leaving this here for karma-whoring completeness reasons.
这篇关于正则表达式检查有效的URL是否以.jpg,.png或.gif结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!