Disable links to images only if link is an image

I am not a professional when it comes to regular expressions but this should be able to catch all linked images which the href attribute links to an image file ((png|jpg|gif|jpeg|bmp) feel free to add/remove extensions):

add_filter('the_content', function($c){
    return preg_replace_callback("#<\s*?a\b[^>]*>(.*?)</a\b[^>]*>#s", function($m){
        $tpl = $m[0];
        $cnt = isset($m[1]) ? $m[1] : null;

        if ( preg_match('/<a(.*?)?href=[\'"]?.+\.(png|jpg|gif|jpeg|bmp)[\'"]?(.*?)?><\/a>/i', str_replace($cnt, '', $tpl)) ) {
            return $cnt;
        } else {
            return $tpl;
        }
    }, $c);
});

I tested it locally and it is working, let me know if that helps.