• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 544
  • Last Modified:

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

0
ModifyMe
Asked:
ModifyMe
  • 12
  • 4
  • 4
  • +1
1 Solution
 
käµfm³d 👽Commented:
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

0
 
käµfm³d 👽Commented:
Sample one was supposed to read:
var validRegExp:RegExp = new RegExp("^<quiz>$");

Open in new window

0
 
ModifyMeAuthor Commented:
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>
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
käµfm³d 👽Commented:
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

0
 
ModifyMeAuthor Commented:
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

0
 
wdosanjosCommented:
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?
0
 
Pravin AsarPrincipal Systems EngineerCommented:
Following should work


public function Trace(evt:KeyboardEvent):void {
      var trg:TextArea = evt.currentTarget as TextArea;
      var data:String = trg.text.toString();
      var patternQuiz:RegExp = /\<quiz\>/;
      if (patternQuiz.test(data)) {
            mx.controls.Alert.show("<quiz>","Quiz Alert",Alert.OK);
      }
}

<mx:TextArea keyUp="Trace(event)" width="100" height="200"/>
0
 
ModifyMeAuthor Commented:
@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.
0
 
ModifyMeAuthor Commented:
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
0
 
Pravin AsarPrincipal Systems EngineerCommented:

>>  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.
0
 
ModifyMeAuthor Commented:
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>
0
 
ModifyMeAuthor Commented:
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.
0
 
ModifyMeAuthor Commented:
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
0
 
Pravin AsarPrincipal Systems EngineerCommented:
< and > are special characters.

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

More learning at

http://www.regular-expressions.info/characters.html
0
 
ModifyMeAuthor Commented:
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.
0
 
ModifyMeAuthor Commented:
@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?

0
 
ModifyMeAuthor Commented:
Code works, I did not fully implment as I should have...my fault.

Thank you!
0
 
käµfm³d 👽Commented:
< 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  = )
0
 
ModifyMeAuthor Commented:
@kaufmed - Thank you for the great info!
0
 
Pravin AsarPrincipal Systems EngineerCommented:
Guys,

here is a good interactive "RegEx Explorer'

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

0
 
ModifyMeAuthor Commented:
Oh wow, thanks!
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 12
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now