0

I have a contenteditable area and it may have something like

Hello @abc @my| it was nice to see you

The | is my cursor, and want to get the text from the '@' symbol nearest to the cursor to the cursor position, so in this case, I want to get '@my'. I have completely no idea how can I get the text backward from a position.

halfer
  • 19,824
  • 17
  • 99
  • 186
user2335065
  • 2,337
  • 3
  • 31
  • 54

1 Answers1

0

given the caret position as caretpos, and value of textbox as text

var result = /\S+@/.exec(text.slice(0, caretpos));
 var lastpos = result.length>0 ? result.lenth - 1:0;
var lastWord = result ? result[lastpos] : null;
snehatulsi
  • 251
  • 1
  • 12