Javascript Image Url Verify

后端 未结 4 1522
温柔的废话
温柔的废话 2020-11-27 02:49

I need to verify an image url to check whether the url is an image of any of these extensions:- jpeg, jpg, gif, png. Example:- when we verify this url http://www.example.com

4条回答
  •  余生分开走
    2020-11-27 03:22

    let url = "folder/myImage.png";
    if ( isValidImageURL(url) ){
        // do something if url is valid image url.
    }
    
    function isValidImageURL(str){
        if ( typeof str !== 'string' ) return false;
        return !!str.match(/\w+\.(jpg|jpeg|gif|png|tiff|bmp)$/gi);
    }
    

提交回复
热议问题