回答
也许你可以在正则表达式之前加上一个点.
,例如:
/\.(jpg|gif|png)$/.test(..
这会尝试在URL中匹配.jpg
,.gif
和.png
。
您提到的正则表达式虽然尝试仅在URL末尾搜索jpg
,gif
和png
,所以我不确定它是如何匹配的http://www.example.com/jpg/bpgpage/
。
如果ommit点.
不过,它仍然会匹配http://www.example.com/jpg
为什么不使用SUBSTR拉最后三个字符?
url.substr(url.length-3, 3);
那个uri:http://meow.com/kitten.png?somehorribleparameter=1 – dino
这里就是我会做,其中“海峡”是你匹配的字符串:
return (str.match(/\.(jpg|gif|png)$/)!= null);
前提是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;
}
}
例:
好的。尽管如此,我会在分割点之前删除'?'部分。 – mavrosxristoforos
@mavrosxristoforos哈哈是我在示例中做到了,现在在答案代码中进行更新;) –
// function for check the type of any source
function checkExtension(file) {
var extension = file.substr((file.lastIndexOf('.') + 1));
switch (extension) {
case 'jpg':
case 'png':
case 'gif':
return "img" // There's was a typo in the example where
break; // the alert ended with pdf instead of gif.
case 'mp4':
case 'mp3':
case 'ogg':
return "video"
break;
case 'html':
return "html"
break;
}
};
var ex = checkExtension("if-a-link-is-an-image.png")
console.log(ex)
- 1. 如何检查(https://)是否为图片或网页链接
- 2. PHP:检查链接是否为图像并检查是否存在
- 3. 检查超链接是否为文件
- 4. 检查图片框值是否为空
- 5. 检查链接是否是外部的
- 6. 检查选择是否包含链接
- 7. 检查目录是否符号链接?
- 8. 检查链接是否重定向
- 9. 检查链接是否包含javascript
- 10. php检查链接是否被点击
- 11. php检查链接是否被点击
- 12. jquery检查是否以链接开始
- 13. 是否有链接检查器吞咽?
- 14. 检查链接列表是否回文
- 15. 检查一张图片是否是另一张图片
- 16. 检查图片是否存在 - iPhone
- 17. 检查图片是否相同
- 18. Android:检查图片值是否匹配
- 19. Safari ClipboardEvent.clipboardData检查图片是否粘贴
- 20. C#检查是否有图片应用
- 21. 如何检查图片是否加载?
- 22. 检查照片库是否为空
- 23. 如何检查链表是否为空
- 24. 图片链接改为存在图片
- 25. 使用FTP上传图片jquery检查图片是否加载
- 26. 如何检查Google街景图片API是否返回图片?
- 27. 图片作为链接[Rails]
- 28. 为什么JavaScript很难检查图片是否被加载?
- 29. PHP,HTML检查文件是否为图片
- 30. 图片链接
这里的美元符号意味着它只能在URL的END处找到扩展名,所以它不会匹配任何包含其中的扩展名的URL。 –
我知道,但它仍然会匹配作为目录的“http:// www.example.com/jpg”。 – mavrosxristoforos
我错过了我的代码中的'$'。 x_x – laggingreflex