ForumsProgramming ForumProblem with Regular Expressions

6 2740
IQAndreas
offline
IQAndreas
299 posts
Peasant

I'm trying to format code so that when it is displayed in a results window, it will be blue for keywords, gray for comments, and red for strings, similar to what Flash does when you write Action Script.

However, I'm having some troubles, and nothing seems to work. What am I doing wrong? Note that this is just a first draft, and still needs to be modified so that it does not highlight "to" in "stop".

public function CodeHTML():String
{
var HTML_Code:String = Code;

//These reserved words were all found in the Adobe Programming Action Script 3 Help reference
var ReservedWords:RegExp = /(as|break|case|catch|class|const|continue|default|delete|do|else|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|internal|is|native|new|null|package|private|protected|public|return|super|switch|this|throw|to|true|try|typeof|use|var|void|while|with)/
HTML_Code = HTML_Code.replace(ReservedWords, "<span style=\\"color: #3300ff\\">$1</span>&quot; //Blue

var SyntacticKeywords:RegExp = /each|get|set|namespace|include|dynamic|final|native|override|static/
HTML_Code = HTML_Code.replace(SyntacticKeywords, "<span style=\\"color: #3300ff\\">$1</span>&quot; //Blue

//Multiline Comments
var MultilineComments:RegExp = /\\/\\*(.*?)\\*\\//s
HTML_Code = HTML_Code.replace(MultilineComments, "<span style=\\"color: silver\\">$1</span>&quot; //Grey

//Single line comments
var Comments:RegExp = /\\/\\/(.*?)/
HTML_Code = HTML_Code.replace(Comments, "<span style=\\"color: silver\\">$1</span>&quot; //Grey

//Strings
var Strings:RegExp = /(["'])(.*?)$1/
HTML_Code = HTML_Code.replace(Strings, "<span style=\\"color: #cc0033\\">$2</span>&quot; //Reddish


//These are available as well, but ignore them for now.
//var FutureReservedWords:RegExp = //

return HTML_Code;

}
  • 6 Replies
IQAndreas
offline
IQAndreas
299 posts
Peasant

Okay. I realized my problem (several, in fact).

Here is the updated code (the regular expressions only to save space):

ReservedWords:RegExp = /\\W(as|break|case|catch|class|const|continue|default|delete|do|else|extends|false|finally|for|function|if|implements|import|in|instanceof|interface|internal|is|native|new|null|package|private|protected|public|return|super|switch|this|throw|to|true|try|typeof|use|var|void|while|with)\\W/sg
SyntacticKeywords:RegExp = /\\Weach|get|set|namespace|include|dynamic|final|native|override|static\\W/sg
MultilineComments:RegExp = /\\/\\*(.*?)\\*\\//sg
Comments:RegExp = /\\/\\/(.*?)/g
Strings:RegExp = /("|'(.*?)$1/sg


But the other error I realized was that the flash TextArea does not support <span> commands.

How else could I change the color of the text that would work with the Flash Text Area?
Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

Guess .color = number; won't do?

IQAndreas
offline
IQAndreas
299 posts
Peasant

I checked on the AS3 LaCR (Language and Components Reference), and you are correct, Captain. The correct syntax is to use <font color='#FFFFFF'></font>

However, now I have run into another snag.

When I use the trace statement, I get this result:

Player_mc.onEnterFrame = function() {
<font color='#CCCCCC'"></font>Lives if (Lives == 0) {
Heart1_mc._visible = false;
Heart2_mc._visible = false;
Heart3_mc._visible = false;
gotoAndStop(2);
}


However, all it says in the TextArea is:
Player_mc.onEnterFrame = function() {

Nothing after that shows up, even though it is there in the trace statement.

Anyone know why I am getting this problem?
Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

Isn't it just a problem of only showing a single line? Try adding a different trace for each, maybe it's not the code

Sssssnnaakke
offline
Sssssnnaakke
1,036 posts
Scribe

As it looks like it is defiently 1/2 Of it.

IQAndreas
offline
IQAndreas
299 posts
Peasant

Okay. I'm not sure exactly what I did, but I was finally able to solve the problem.

I posted my code here:
http://www.geocities.com/rubikssites/RegularExpressionProblem.txt

Now I only have one problem left (which isn't really a priority).
Keywords, such as of, as, if, etc, show up in blue, even when in the comments. Is there any way to get around this?

One way is to replace all comments with something like <com001>, <com002>, <com003>, and then later go back and replace all of those with the actual values stored in arrays, but is there any better or easier way to do this?

Showing 1-6 of 6