According to the search() documentation here:
The optional parameter endpos limits how far the string will be searched; it will be as if the string is endpos characters long, so only the characters from pos to endpos – 1 will be searched for a match.
So your syntax, a.search("cat", 0, 2) is equivalent to a.search("ca"), which does match the pattern a$.