Using a variable in AS3, Regexp

Using ActionScript 3.0 (inside Flash CS5)

Standard regex to match any digit:

var myRegexPattern:Regex = /\d/g; 

What will a regular expression look like to include a string variable? (this example is an IDEAL, not a "WORKING" snippet), for example:

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

I saw some workarounds that involve creating multiple instances of regular expressions and then combining them by source with the variable in question, which seems to be wrong. OR using a flash string for the regex maker, but it's just messy with all the necessary double and triple escape sequences.

There must be some painful way that I cannot find in live documents or on Google. Does AS3 even support this functionality? If not, it really should be.

Or am I missing much simpler means to just avoid this task, that I'm just naive too because of my novelty to regex?

+6
source share
2 answers

Actually, I wrote about this on blogs, so I’ll just point you out: http://tyleregeto.com/using-vars-in-regular-expressions-as3 It talks about possible solutions, but there’s no ideal one, as you mention .

EDIT

Here is a copy of the important parts of this blog post:

Here is a regular expression to cut tags from a block of text.

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

This great expression works like a charm. But I would like to update it so that the developer can limit the tags that he has split to those that are specified in the array. Pretty straight forward, to use a variable value in a regular expression, you first need to build it as a string, and then convert. Something like the following:

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

Pretty straight forward. So when we get closer to this small update, this is what I did. Of course, one big problem was pretty clear.

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

Guess what, the wrong line! Better avoid these quotes in a line. Oops, this will break the regex! I was at a dead end. So I opened a link to the language to find out what I can find. The "source" parameter, which I had never used before, caught my eye. It returns a string described as "part of a regular expression pattern". He did a great job with this. Here is the solution:

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

You can reduce this for convenience:

 var complete:RegExp = new RegExp(/])*>/.source + tag, 'ig'); 
+9
source

As Tyler correctly points out (and his answer works just fine), you can collect your regular expression as the end of a line, and then pass that line to the RegExp constructor with the syntax new RegExp("pattern", "flags") .

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

Note that when using a string to store a regular expression pattern, you need to add additional backslashes to make it work correctly (for example, to get \d in a regular expression, you need to specify \\d in the string). Also note that the line pattern does not use forward slashes. In other words, the following two statements are equivalent:

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

Note: you may need to process the myString variable to avoid any backslashes that it might contain (if you should interpret them as a literal). If so, the function becomes:

 function assembleRegex(myString) { myString = myString.replace(/\\/, '\\\\'); var re = new RegExp('\\d' + myString); return re; } 
+6
source

Source: https://habr.com/ru/post/892535/


All Articles