Link to home
Start Free TrialLog in
Avatar of ModifyMe
ModifyMe

asked on

Regular Expression in Actionscript (Flash CS5)

Hey all,

I am trying to get a trace event to occur when the text <quiz> is shown in a textarea.  Currently, if works if I use /"quiz"/, but I just want to have <quiz>...nothing else.

My code:
import fl.controls.Button;
import fl.controls.TextInput;
import fl.controls.TextArea;
import flash.events.MouseEvent;

var btn1:Button;
var ti1:TextInput
var ta1:TextArea;

btn1.addEventListener(MouseEvent.CLICK, mouseMe);
function mouseMe(Event:MouseEvent):void
{
	ta1.text = ti1.text;
	
	var validRegExp:RegExp = new RegExp(/"quiz"/);	
	if(ta1.text == String(validRegExp))
	   {
		   trace("Regular Expression:  " + validRegExp);
	   }
}

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Try either of the following (I'm not sure which you are after since you say, "I just want to have <quiz>...nothing else"):

var validRegExp:RegExp = new RegExp(/"^<quiz>$"/);

Open in new window


var validRegExp:RegExp = new RegExp("^\s*<quiz>\s*$");

Open in new window

Sample one was supposed to read:
var validRegExp:RegExp = new RegExp("^<quiz>$");

Open in new window

Avatar of ModifyMe
ModifyMe

ASKER

Hmmm, well I tried both and it did not work.

I want to be able to type in: <quiz>
....to be recognized as reg exp.


Including the less-than and greater-than signs.

<quiz>
I want to be able to type in: <quiz>
....to be recognized as reg exp.
Ah. I though you meant you wanted to match "<quiz>" as the only text in the box. That's what the previous submissions were intended to do. You should just be able to enter the string literally:
var validRegExp:RegExp = new RegExp("<quiz>");

Open in new window

Hmmmm, am I missing something?  I thought the same too, but I do not get the trace to work.

import fl.controls.Button;
import fl.controls.TextInput;
import fl.controls.TextArea;
import flash.events.MouseEvent;

var btn1:Button;
var ti1:TextInput
var ta1:TextArea;

btn1.addEventListener(MouseEvent.CLICK, mouseMe);
function mouseMe(Event:MouseEvent):void
{
	ta1.text = ti1.text;
	
	var validRegExp:RegExp = new RegExp("<quiz>");
	if(ta1.text == String(validRegExp))
	   {
		   trace("Regular Expression:  " + validRegExp);
	   }
}

Open in new window

What String(validRegExp) returns?

I'm not sure I understand what you are trying to accomplish with 'if(ta1.text == String(validRegExp))'.  Can you elaborate?
ASKER CERTIFIED SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@wdosanjos - I am checking to see if the TextArea (ta1) text equals the value of the Regular Expression which had to be turned into a String due to type issues.

@pravinasar - I am doing this in Flash not Flex.  Also, I did add your code:
var patternQuiz:RegExp = /\<quiz\>/;

Open in new window

...and it gave me /\<quiz\>/ in the trace window.
Attached is a screenshot, as you can see...if the text is enter EXACTLY as coded, then the trace statement works, but I don't want all of that in the text.

I only want:    <quiz>
RegExp.jpg

>>  but I don't want all of that in the text.

I am not getting it. What you do not want ?



Flex is based on Flash & ActionScript

So, RegEx will work.
I do not want:  /\<quiz\>/
I want:  <quiz>

As you can see, the output is exactly what the code is for the Reg Exp.  I only want <quiz>

The only way the trace works is if the text input box is exactly what the code is...but again, I only want <quiz>
I just don't know why the forward and back slashes are showing up, I thought the Reg Exp would use those as flags and not display them.
Even using:  
var validRegExp:RegExp = /<quiz>/;

Open in new window

won't work unless the forward slashes are used in the text input box.

I just want to enter <quiz> in the text input box w/o the forward slashes
< and > are special characters.

For the RegEx parser to interpret these as <, you need \

More learning at

http://www.regular-expressions.info/characters.html
Let me try to be a bit more clear.

Let's say I have a paragraph like the following:

Today is the day I will try and make things work with what I have learned.  I will need to test my knowledge <quiz>

So if that paragraphs is in the text area, I want to look for the metatag <quiz> and have a trace statement occur as long as it finds <quiz> upon clicking the button.
@pravinasar - yes, I do understand that, but for my trace to work, I even have to type in the forward slashes.  I thought the forward slashes were to indicate the beginning and end of a Reg Exp?

Code works, I did not fully implment as I should have...my fault.

Thank you!
< and > are special characters.

For the RegEx parser to interpret these as <, you need \

More learning at

http://www.regular-expressions.info/characters.html
Uhhh....  no they're not--at least not as far as regex is concerned. The angle brackets may be special characters as far as Flex is concerned (I don't know as I am not a Flex programmer), but I don't see anything in the documentation that indicates they are special with regard to regex. If this is the case, then the link you posted is misleading.

*Some* engines, like text editors, do give special meaning to angle brackets in their own regex implementations. As I said, I didn't see anything in the docs to indicate that was the case  here.

Since I am not a Flex programmer, I will gracefully admit defeat if I am wrong  = )
@kaufmed - Thank you for the great info!
Guys,

here is a good interactive "RegEx Explorer'

http://ryanswanson.com/regexp/#start

Oh wow, thanks!