如何在flex 4中的textarea中的当前光标位置下获取单词

时间:2011-10-02 02:41:31

标签: actionscript-3 flex4 textarea

我正在使用flex4创建编辑器。我需要在当前光标位置下获取单词。例如,这是textarea中的文本,“嗨,这是一个样本”,光标位于“this”字下。如果我单击一个按钮,则必须返回此“this”字样。所有这些我需要在flex4和actionscript3中实现。请提供任何建议或帮助。

2 个答案:

答案 0 :(得分:0)

看看TextField.caretIndex。它是文本字段中光标的索引位置。然后,您需要使用该位置提取当前单词。一种方法是在两个方向上寻找空间,但我确信有一些花哨的正则表达式可以使用插入符号索引为您完成。

答案 1 :(得分:0)

我需要这样的代码提示功能,这种方法效果很好:

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);
}