How to check if a URL is an image in JavaScript

1 Answer

0 votes
function isImage(url) {
  	return /\.(jpg|jpeg|png|bmp|webp|apng|avif|gif|svg)$/.test(url);
}

console.log(isImage('https://collectivesolver.com/images/grammarly-grammar-checker.png'));

console.log(isImage('https://seek4info.com/search.php?query=web+hosting'));







  
/*
run:
  
true
false
  
*/

 



answered Apr 28, 2022 by avibootz

Related questions

1 answer 118 views
1 answer 150 views
1 answer 170 views
2 answers 220 views
3 answers 338 views
1 answer 366 views
...