I fixed this issue for mentions last week in this commit https://github.com/discourse/discourse/commit/b9891e3def289d2d0f33c0f18fa0bcd1e724e390.
The fix for category autocomplete is pretty much the exact same. Here is a PR https://github.com/discourse/discourse/pull/10457.
Merged!