简体   繁体   English

如何从可编辑区域开始在光标位置之前获取文本

[英]How can I get the text right before the cursor position starting from a contenteditable area

I have a contenteditable area and it may have something like我有一个 contenteditable 区域,它可能有类似的东西

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'.是我的光标,并且想要从离光标最近的“@”符号到光标位置获取文本,因此在这种情况下,我想要获取“@my”。 I have completely no idea how can I get the text backward from a position.我完全不知道如何从一个位置向后获取文本。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 contenteditable 中不包括标签的文本中获取光标位置? - How to get cursor position in text within contenteditable excluding tags? 在js中可编辑的段落内容中编辑文本后,如何在任意位置移动光标 - how can I move cursor at any position after editing text at paragraph contenteditable in js 如何从 contenteditable 获得“删除插入符号位置”? - How can I get the "drop caret position" from contenteditable? 获取包含HTML内容的contentEditable区域中的插入符(光标)位置 - Get caret (cursor) position in contentEditable area containing HTML content 如何获取包含HTML的contentEditable中的光标位置? - How to get the cursor position in contentEditable containing HTML? 如何检查文本 cursor 是否在 contenteditable div 内? - How can I check if the text cursor is inside of a contenteditable div? 如何获得一个可疑的html元素的光标行号? - How can i get the cursor line number of a contenteditable html element? 可信的文本编辑器和光标位置 - Contenteditable text editor and cursor position 如何在 contenteditable div 中检测光标位置并在该位置插入文本 - How to detect cursor position in contenteditable div and insert text at that position 如何在 TextBox(用户、密码等)中缩进文本和光标的起始位置 - How can I indent the text, and starting position of cursor, in a TextBox (user, password, etc..)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM