Strange RegExp & Array issue

I just don't see it.... if I run the startReading() function repetetively, the [ ] around the word keep disappearing and coming back. I want to use RegExp to check for some "text modifiers", and use a function selectRegEx to see what RegExp is applicable. This is what is used to convert the raw input from the array to the output in the TextArea.

But what do I do wrong?  Thanks a lot!!

Pieter
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="startReading()" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#F0EA8A, #55510E]">
	<mx:Script>
		<![CDATA[
			private var regexOPEN	: RegExp = /#(.*?)#/g;			// filters for #...#
			private var regexMC		: RegExp = /\[#(.*?)#\]/g;		// filters for [#...#]
			private var regexNEG	: RegExp = /\[!#(.*?)#\]/g;		// filters for [!#...#]
		
			private var testData1: Array = ["[#regexMC#]"];
			private var testData2: Array = ["[!#regexNEG#]", "[#regexMC#]"];
			private var testData3: Array = ["[!#regexNEG#]", "[#regexMC#]", "#regexOPEN#"];
			private var testData4: Array = ["[!#regexNEG#]", "#regexOPEN#", "[#regexMC#]"];
			
			private function selectRegEx(rawText: String): RegExp {
				var regexOutput: RegExp = new RegExp();
				// check regexOPEN last (will also test "true" for MC/NEG and falsely provide output)
				if (regexNEG.test(rawText)) {
					regexOutput = regexNEG;
				} else if (regexMC.test(rawText)) {
					regexOutput = regexMC;
				} else if (regexOPEN.test(rawText)) {
					regexOutput = regexOPEN;
				} else {
					regexOutput = regexOPEN; // just taking a default to garantee output
				}
				return regexOutput;
			}
			
			// create text for reading mode (strip text modifiers)
			private function convertToRead(rawText: String): String {
				var strippedText: String;
				strippedText = rawText.replace(selectRegEx(rawText), "$1");
				return strippedText;
			}
			
			private function startReading(): void {
				var readItems: String = "";
				for each (var item: String in testData1) {
					readItems += convertToRead(item) + "<br/><br/>";
				}
				txaItem.htmlText = "<br/>" + readItems;
			}
		]]>
	</mx:Script>
	<mx:Panel width="447" height="294" layout="absolute" horizontalCenter="0" verticalCenter="-20">
		<mx:TextArea width="407" height="182" id="txaItem" textAlign="center" fontSize="16" fontWeight="bold" x="10" y="10"/>
		<mx:Button x="151.5" y="200" label="Test" width="124" height="44" fontSize="18" click="startReading()"/>
	</mx:Panel>
</mx:Application>

Open in new window

pieter78Asked:
Who is Participating?
 
pieter78Connect With a Mentor Author Commented:
All right, I have it solved, but using a different approach. Either it is a bug in AS3 or a bug in me, but I just cannot see what I did wrong.

If I use code example 1 (see snippet) where I apply the RegExp.test(String) function, things go messy.
If I use code example 2 (snippet) applying String.search(RegExp) then it works.

In some way -which I find strange- the Boolean output of the test() function causes the issue. I I add some trace() comments during testing where I use this test() as well, the whole output of the function gets influenced!

I simply suggest not using test() anymore, but instead check whether String.search(RegExp) > -1

Pieter
Code example 1 (NOT WORKING):
if (regexNEG.test(rawText)) {
   regexOutput = regexNEG;
} else if (regexMC.test(rawText)) {
   regexOutput = regexMC;
} else if (regexOPEN.test(rawText)) {
   regexOutput = regexOPEN;
}
 
Code example 2 (works):
if (rawText.search(regexNEG) > -1) {
   regexOutput = regexNEG;
} else if (rawText.search(regexMC) > -1) {
   regexOutput = regexMC;
} else if (rawText.search(regexOPEN) > -1) {
   regexOutput = regexOPEN;
} 

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.