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:
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);
}
}
Sample one was supposed to read:
var validRegExp:RegExp = new RegExp("^<quiz>$");
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.
Including the less-than and greater-than signs.
<quiz>
I want to be able to type in: <quiz>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:
....to be recognized as reg exp.
var validRegExp:RegExp = new RegExp("<quiz>");
ASKER
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);
}
}
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?
I'm not sure I understand what you are trying to accomplish with 'if(ta1.text == String(validRegExp))'. Can you elaborate?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@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:
@pravinasar - I am doing this in Flash not Flex. Also, I did add your code:
var patternQuiz:RegExp = /\<quiz\>/;
...and it gave me /\<quiz\>/ in the trace window.
ASKER
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
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.
ASKER
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 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>
ASKER
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.
ASKER
Even using:
I just want to enter <quiz> in the text input box w/o the forward slashes
var validRegExp:RegExp = /<quiz>/;
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
For the RegEx parser to interpret these as <, you need \
More learning at
http://www.regular-expressions.info/characters.html
ASKER
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.
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.
ASKER
@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?
ASKER
Code works, I did not fully implment as I should have...my fault.
Thank you!
Thank you!
< and > are special characters.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.
For the RegEx parser to interpret these as <, you need \
More learning at
http://www.regular-expressions.info/characters.html
*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 = )
ASKER
@kaufmed - Thank you for the great info!
ASKER
Oh wow, thanks!
Open in new window
Open in new window