If the text the user sees remains the same (which your question seems to imply is the case), you could use a character offset-based solution. I’ve posted some code for that elsewhere on Stack Overflow:
https://stackoverflow.com/a/13950376/96100
I also answered a related question quite recently: