I figured this would be a cute little way to show the very basics of matching some text, setting up TextFormats and applying them to key areas.
Knocked this out pretty quickly so let me know if you notice any bugs.
Originally Written in 2011
Edited May 5 2014, to update the email
package {
import flash.events.Event;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public var i:TextField, //pattern input
b:TextField, //text that the pattern is matched against, editable
ps:String,
bs:String,
tfBBU:TextFormat,//bold blue underlined
tfIB:TextFormat;//italic blue
public function matchOverBody():void{
//As I don't want to force the user to type in the foreward slashes
//and I don't feel like doing the controls to effect the flags right now,
//we will just assume the flags are: gxm
var r:RegExp;
var m:Array = []; //the match object can also be an array so...
r = new RegExp(ps,'gxm');
m = r.exec(bs);
//adding restrictions in case the loop goes too far
for(var i:uint = 0; m!= null && i != bs.length; ++i){
if(m.index!= m.index + m[0].length){
b.setTextFormat(tfBBU,m.index , m.index+m[0].length);
}else{
break;
}
m = r.exec(bs);
}
}
public function updateTxt():void{
ps = i.text;
bs = b.text;
b.setTextFormat(tfIB,0,b.text.length);
if(ps!=''){
matchOverBody();
}
}
public function changeH(e:Event):void{//change handler
if(e.target == b || e.target == i){
updateTxt();
}
}
public function FlashTest() {
//setting up the textFields
i = new TextField();
i.height = 20;
i.width = stage.stageWidth - 4;
i.y = stage.stageHeight - 24;
i.type = 'input';
i.border = true;
i.x = 2;
b = new TextField();
b.text = "Here is where you put the text that you check against your pattern. \nAssume gxm are the flags.\n\nTo get in touch with me, you can email me at: ap13mo" + "@" + "student.ocadu.ca";
b.height = stage.stageHeight - 32;
b.width = i.width;
b.border = true;
b.multiline = true;
b.type = 'input';
b.y = 4;
b.x = i.x;
//listen for changes to the text objects
stage.addEventListener(Event.CHANGE, changeH);
//setup for bold n blue
tfBBU = new TextFormat();
tfBBU.color = 0x0066cc;
tfBBU.bold = true;
tfBBU.italic = false;
tfBBU.underline = true;
//setup for italic n black
tfIB = new TextFormat();
tfIB.color = 0x000000;
tfIB.bold = false;
tfIB.italic = true;
tfIB.underline = false;
//fill it with an example pattern
i.text = "(?:[a-zA-Z0-9]+\\.)*(?:[a-zA-Z0-9]+)@(?:[a-zA-Z0-9]+\\.)+[a-zA-Z][a-zA-Z]+";//
//apply the pattern and modify the TextFormat of sections found
updateTxt();
//place the textfields on the display list to make them visible
addChild(b);
addChild(i);
}
}
}