It looks like replaceWith actually can accept a function. In that case it will work like this:
var a = $('a');
As you can see the function receives the index of the matched element. It would be even handier if the element would be passed to the callback as a second parameter. Then you could avoid a hack like above.
It would be nice if you could either document the usage or improve it as suggested above.