我使用flex4来创建编辑器。在那里我需要获取当前光标位置下的单词。例如,这是文本区域中的文本,"Hi,this is a sample“,光标位于" this”单词下。如果我点击一个按钮,那么这个" this“字必须返回。所有这些我都需要在flex4和actionscript3中实现。请提供任何建议或帮助。
发布于 2011-10-02 14:10:43
看看TextField.caretIndex吧。它是光标在文本字段中的索引位置。然后,您需要使用该位置提取当前单词。一种方法是在两个方向上寻找空格,但我相信有一些花哨的正则表达式可以使用插入符号索引为您做这件事。
发布于 2013-02-07 19:16:04
我需要一个这样的函数来进行代码提示,这种方法工作得很好:
public function currentWord(ta:RichEditableText):String
{
var wordBegin:int = ta.selectionAnchorPosition;
var wordEnd:int = ta.selectionAnchorPosition;
// while the previous char is a word character... let's find out where the word starts
while (ta.text.charAt(wordBegin-1).search(/\w+/) > -1 && wordBegin > 0 ) { wordBegin--; }
// while the next char is a word character... let's find out where the word ends
while (ta.text.charAt(wordEnd).search(/\w+/) > -1 && wordEnd > 0 ) { wordEnd++; }
return ta.text.substring(wordBegin, wordEnd);
}https://stackoverflow.com/questions/7624290
复制相似问题