It's complicated, because of a fairly continuous series of changes from 1995.5 - 2004. Most all these changes related to engine control, ignition system, fuel injectors, sensors, timing, port design on the heads, emission controls, etc., etc. In other words, the block remained pretty much the same, but a lot of surrounding apparatus changed from 1995.5 through 2004.
The most direct whole-engine drop-in replacements for your 1998 Taco are limited to 2 model years - 1998 and 1999.
Prior to 1998 the ignition system used a distributor, so you'd be doing some significant ignition conversion to use a 1995.5 - 1997 engine. Also, the engine control and fuel injection systems were changed for 1998.
In 2000 the cylinder head port design, injection system, and miscellaneous other components changed to support the LEV emissions standards on California spec models, and the EGR system was discontinued. These changes carried on through the 2001 - 2004 era.
I'm not certain whether there were any significant differences between 1998 and 1999. I don't think there were, but I don't have the documentation to prove it.
Anyway ... Like I said above - the most straightforward drop-in replacements are 1998 and 1999. Anything else is going to involve varying degrees of modifications / adaptations / frankensteining.
I've got a 1999 Tacoma 2.4 that I bought brand new and the reply that you got from Enola G is correct 98' and 99' will be the years you want to look for, if you try any other years for a swap you'll encounter all sorts of issues I've helped with one of those and it ended up being a whole bag of worms. Good luck