Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Variable in an AS3, Regexp

Using Actionscript 3.0 (Within Flash CS5)

A standard regex to match any digit is:

var myRegexPattern:Regex = /\d/g;

What would the regex look like to incorporate a string variable to match? (this example is an 'IDEAL' not a 'WORKING' snippet) ie:

var myString:String = "MatchThisText"
var myRegexPatter_WithString:Regex = /\d[myString]/g;

I've seen some workarounds which involve creating multiple regex instances, then combine them by source, with the variable in question, which seems wrong. OR using the flash string to regex creator, but it's just plain sloppy with all the double and triple escape sequences required.

There must be some pain free way that I can't find in the live docs or on google. Does AS3 hold this functionality even? If not, it really should.

Or I am missing a much easier means of simply avoiding this task that I'm simply naive too due to my newness to regex?

like image 966
Adrian Seeley Avatar asked Jul 11 '11 21:07

Adrian Seeley


People also ask

How do I create a regexp object in ActionScript?

You can create a new RegExp object by using the new RegExp () constructor or by assigning a RegExp literal to a variable: var pattern1:RegExp = new RegExp ("test-\d", "i"); var pattern2:RegExp = /test-\d/i; For more information, see "Using Regular Expressions" in the ActionScript 3.0 Developer's Guide.

What are regular expressions in ActionScript 3?

For more information, see "Using Regular Expressions" in the ActionScript 3.0 Developer's Guide. A reference to the class object or constructor function for a given object instance. [read-only] Specifies whether the dot character (.) in a regular expression pattern matches new-line characters.

How to use variables in Python regex?

Python Server Side Programming Programming The following code demonstrates the use of variables in python regex. The variable cannot contain any special or meta characters or regular expression. We just use string concatenation to create a string.

How to pass a variable to the regex literal pattern?

If we try to pass a variable to the regex literal pattern it won’t work. The right way of doing it is by using a regular expression constructor new RegExp ().


2 Answers

I've actually blogged about this, so I'll just point you there: http://tyleregeto.com/using-vars-in-regular-expressions-as3 It talks about the possible solutions, but there is no ideal one like you mention.

EDIT

Here is a copy of the important parts of that blog entry:

Here is a regex to strip the tags from a block of text.

/<("[^"]*"|'[^']*'|[^'">])*>/ig

This nifty expression works like a charm. But I wanted to update it so the developer could limit which tags it stripped to those specified in a array. Pretty straight forward stuff, to use a variable value in a regex you first need to build it as a string and then convert it. Something like the following:

var exp:String = 'start-exp' + someVar + 'more-exp';
var regex:Regexp = new RegExp(exp);

Pretty straight forward. So when approaching this small upgrade, that's what I did. Of course one big problem was pretty clear.

var exp:String = '/<' + tag + '("[^"]*"|'[^']*'|[^'">])*>/';

Guess what, invalid string! Better escape those quotes in the string. Whoops, that will break the regex! I was stumped. So I opened up the language reference to see what I could find. The "source" parameter, (which I've never used before,) caught my eye. It returns a String described as "the pattern portion of the regular expression." It did the trick perfectly. Here is the solution:

var start:Regexp = /])*>/ig;
var complete:RegExp = new RegExp(start.source + tag + end.source);

You can reduce it down to this for convenience:

var complete:RegExp = new RegExp(/])*>/.source + tag, 'ig');
like image 199
Tyler Egeto Avatar answered Oct 14 '22 17:10

Tyler Egeto


As Tyler correctly points out (and his answer works just fine), you can assemble your regex as a string end then pass this string to the RegExp constructor with the new RegExp("pattern", "flags") syntax.

function assembleRegex(myString) {
    var re = new RegExp('\\d' + myString, "i");
    return re;
}

Note that when using a string to store a regex pattern, you do need to add some extra backslashes to get it to work right (e.g. to get a \d in the regex, you need to specify \\d in the string). Note also that the string pattern does not use the forward slash delimiters. In other words, the following two statements are equivalent:

var re1 = /\d/ig;
var re2 = new Regexp("\\d", "ig");

Additional note: You may need to process the myString variable to escape any backslashes it might contain (if they are to be interpreted as literal). If this is the case the function becomes:

function assembleRegex(myString) {
    myString = myString.replace(/\\/, '\\\\');
    var re = new RegExp('\\d' + myString);
    return re;
}
like image 28
ridgerunner Avatar answered Oct 14 '22 18:10

ridgerunner