How can I modify all existing tags while keeping the urls themselves?

I solved the issue with help from this answer: https://wordpress.stackexchange.com/a/159570/176615

// add nofollow to Amazon
function add_nofollow_content($content) {
    $content = preg_replace_callback(
        '/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
    function($m) {
        if (strpos($m[1], "amzn.to") !== false)
            return '<a href="'.$m[1].'" rel="nofollow noopener" target="_blank">'.$m[2].'</a>';
        else
            return '<a href="'.$m[1].'" rel="noopener" target="_blank">'.$m[2].'</a>';
    },
    $content);
    return $content;
}
add_filter('the_content', 'add_nofollow_content');