问题描述
我希望用户提交有效但也是图片的网址,以.jpg,.png或.gif结尾。
(?:([^:/?#] +):)?(?://([?^ /#] *))([^#?] * \\? \\。(?:jpg | gif | png))(?:\?([^#] *))?(?:#(。*))?
这是来自,虽然没有完全回答这个问题,但我认为在这种情况下它可能更有用。但是,我将这里留给完整性原因。
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 completeness reasons.
这篇关于正则表达式检查有效的URL是否以.jpg,.png或.gif结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!