How to use str_replace() outside the loop?

Filters don’t really affect resulting markup. More precisely they affect anything that is passed through them, in this specific case output of the_content() function is passed through the_content filter and you are able to modify it.

If markup you want to change is not generated by function and not passed through filter then most likely you will need to edit theme’s template file that markup belongs to.

It is also good practice to create and use child theme to retain compatibility with theme updates and such if you are going to edit template.