【问题标题】:Identify Certain Text Within Input Text Actionscript 3.0识别输入文本 Actionscript 3.0 中的某些文本
【发布时间】:2026-01-25 01:10:02
【问题描述】:

好的,你好。只是有一个快速的问题要问。基本上,我正在尝试在 Flash 和 Actionscript 中开发一个 TextEditor,例如,我正在尝试做的是确定 "addEventListener" 已输入到我的输入字段中。从那里我想将显示"addEventListener" 的区域的颜色更改为例如蓝色。

所以我只是想知道最基本的方法是什么

【问题讨论】:

  • 您找到解决方案了吗?

标签: actionscript-3 flash text input


【解决方案1】:

首先,您需要找到单词的所有实例,然后为它们设置文本格式。像这样:(假设您的文本输入称为txt

//create a default format that you reset the whole text field to every time you check
var defaultTf:TextFormat = txt.defaultTextFormat;

function highlightWord(word:String, caseSensitive:Boolean = false){ 
  //the current text of the input text field
  var textBlock:String = txt.text;

  //lower case everything if you don't want case sensitivity
  if(!caseSensitive){             
    word = word.toLocaleLowerCase();        
    textBlock = textBlock.toLocaleLowerCase();   
  }    

  //an array to store all the starting indexes of the word
  var arr:Array = new Array();

  //helper vars for the loop below       
  var curIndex:int = 0;    
  var foundIndex:int;

  //loop so long as the word is found (the found index is 0 or greater)
  while(foundIndex > -1){
      foundIndex = textBlock.indexOf(word, curIndex);

      if(foundIndex > -1){
          //add the index of the found word to the array
          arr.push(foundIndex);

          //increment the index by word length
          curIndex = foundIndex + word.length;
      }
  }

  //create a text format to use to highlight the words (blue)
  var tf:TextFormat = new TextFormat(null,null,0x0000FF);

  //set everything to the default style
  txt.setTextFormat(defaultTf);

  //loop through the array of words, and highlight them with the text format
  for(var i:int=0;i<arr.length;i++){
      txt.setTextFormat(tf, arr[i],arr[i] + word.length);
  }
}

highlightWord("addEventListener");

【讨论】:

    【解决方案2】:

    可以使用 RegExp 类获取匹配项和位置:

        // change wholeText to a class variable or function param
        var wholeText:String = "...addEventListener( Event...";
        var pattern:RegExp = /addEventListener/g; 
        var result:Array = pattern.exec( wholeText ); 
    
        while ( result != null ) 
        { 
            trace( result.index, pattern.lastIndex, result ); 
            result = pattern.exec( wholeText ); 
        }
    

    ...要包含格式,您可以...

        var tf:TextFormat = new TextFormat( null, null, 0x0000ff );
        var wholeText:String = "...addEventListener( Event...";
        var pattern:RegExp = /addEventListener/g; 
        var result:Array = pattern.exec( wholeText ); 
    
        while ( result != null ) 
        { 
            wholeText.setTextFormat( tf, result.index, pattern.lastIndex );
            result = pattern.exec( wholeText ); 
        }
    

    我没有针对 BadFeelingAboutThis 的方法进行性能检查。我将其作为更紧凑的解决方案提供。

    当然,你可以很容易地实现它,改变

        var pattern:RegExp = /addEventListener/g;
    

    ...到...

        function colorizeString( findMe:String ) : void
        {
            // ....
            var pattern:RegExp = new RegExp( findMe, "g" );
            // ...
        }
    

    我将其保留为区分大小写。如果您希望它不区分大小写,您可以将“g”更改为“gi”。

    【讨论】: