lag*_*lex 3 javascript regex url image
我需要检查URL是否是图像.
答案在这里
/(jpg|gif|png)$/.test(...
Run Code Online (Sandbox Code Playgroud)
当我的(特殊)情况下,当非图像URL中存在扩展时,会引发误报
http://www.example.com/jpg/bpgpage/
Run Code Online (Sandbox Code Playgroud)
(PS:不能使用jquery,只能使用javascript/regex)
也许您可以.在正则表达式之前添加一个点,例如:
/\.(jpg|gif|png)$/.test( ..
Run Code Online (Sandbox Code Playgroud)
这将尝试在 URL 中匹配.jpg,.gif和.png。
不过,您提到的正则表达式确实尝试搜索jpg,gif并且png仅在 URL 的末尾,所以我不确定它是如何匹配的http://www.example.com/jpg/bpgpage/。
如果你省略了点.,它仍然会匹配http://www.example.com/jpg
如果uri以扩展名结尾,则每次都应该有效.
码:
var isUriImage = function(uri) {
//make sure we remove any nasty GET params
uri = uri.split('?')[0];
//moving on, split the uri into parts that had dots before them
var parts = uri.split('.');
//get the last part ( should be the extension )
var extension = parts[parts.length-1];
//define some image types to test against
var imageTypes = ['jpg','jpeg','tiff','png','gif','bmp'];
//check if the extension matches anything in the list.
if(imageTypes.indexOf(extension) !== -1) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
示例: