How to check if a URL is an image in TypeScript

1 Answer

0 votes
function isImage(url : string ) : boolean {
    return /\.(jpg|jpeg|png|bmp|webp|apng|avif|gif|svg)$/.test(url);
}
 
console.log(isImage('https://collectivesolver.com/images/readingheadstart.jpg'));
 
console.log(isImage('http://answersmind.com/search.php?q=animal%20start%20with%20b'));
 
 
 
 
 
 
 
   
/*
run:
   
true
false
   
*/

 



answered Apr 28, 2022 by avibootz
...