Strange RegExp & Array issue

Posted on 2009-02-08
Last Modified: 2013-11-11
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!!

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="" layout="absolute" creationComplete="startReading()" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#F0EA8A, #55510E]">



			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: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()"/>



Open in new window

Question by:pieter78
    1 Comment

    Accepted Solution

    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 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 > -1

    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 ( > -1) {
       regexOutput = regexNEG;
    } else if ( > -1) {
       regexOutput = regexMC;
    } else if ( > -1) {
       regexOutput = regexOPEN;

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Title # Comments Views Activity
    Flash media server 1 322
    Embedding Flash App/Movie on Website 3 481
    anti-download video publishing site? 6 588
    Action Script commands 3 203
    This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
    Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
    The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
    The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now