Quote from OrionShockLooks like argument overflow from gsub. Not only does it return the fixed up string but also how many replacements it does. The # of replacements is also passed to the string.find() call and is messing it up.
Yep, this was it. The original example I looked at actually had things cached separately; I was just trying to simplify things a bit since I was only using it in the one place. Forgot about the extra argument (/facepalm).
Thanks for your help!
Edit: Though now that I'm thinking about it, I should probably be caching them anyway...