I don't really know how it works, so if more advanced people can confirm or have better ideas, thanks.
Here is a cleaner and more functional version.
calibre://search/_?q=tags:%22=tag%22
(the %22 are important to have the " in the query.)
Code:
<a href="calibre://search/_?q=tags:%22=' & i & '%22">
This should do the vast majority of case you are looking for and if there are any bugs left, it should be a solid base to work from.
I will continue on my side to solve the remaining problems.