I have a div tag with contenteditable set to true.
I am trying to find out the last entered word in the div.
For example, if I type in This is a test and I hit a space, I want to be able to get the word test
I want to be able to use this logic so that I can test each word being typed (after the space is pressed).
It would be great if someone could help me with this.
An easy solution would be the following
trim might need a shim for older browsers. (
.replace(/\s$/,""))To strip punctuation like
" Test!!! "you could additionally do a replace like following:You might want to do a more specific definition of the characters to omit than
\W, depending on your needs.If you want to trigger your eventhandler also on punctuation characters and not only on space, the last replace is not needed.