如何使用 Regex 替换 OnChanging 事件中的字符并将插入符号保留在 ExtendScript 中 EditText 的末尾?

How can I replace a character in an OnChanging event using Regex and keeping the caret at the end of the EditText in ExtendScript?

我需要在 TextBox 中键入内容并立即在 StaticText 中查看结果。为了避免“”(space 字符),我使用正则表达式替换它。 它正在工作,但每次按下 space 键时,光标都会向左移动。它正在被“_”取代,但由于光标移动到我的 EditText 框中的第一个位置,因此无法继续流畅地输入。

editText.onChanging = function(){
  if (this.text.match(/\s*$/i)) {
    this.text = this.text.replace(/ /g, "_");
  }
  staticText.text = this.text;
}

如何输入该编辑文本,替换每个 space 个字符并将光标保持在行尾?

我找到了解决方案。

要使用 Regex 替换字符并将插入符保留在行尾,您应该将替换结果保存在变量中,清除文本框并使用文本选择将其返回到您的编辑文本框.

editText.onChanging = function(){    
     if (this.text.match(/\s*$/i)) {    
          var text = this.text.replace(/ /g, "_");  
          this.text = "";  
          this.textselection = text;  
     }  
     staticText.text = this.text;    
}