Thanks to this question and answer posted by Tim Down, I made a function to get the word preceding caret in a \"contenteditable\" div.
Here\'s a fiddle, and here\'s
You could try using index property of match()
index
var lastWordPosition = preceding.match(/(?:\s|^)([\S]+)$/i).index;
Then you can do a substring(0, lastWordPosition) or whatever you want…
substring(0, lastWordPosition)