在AS3使用一个变量,正则表达式变量、正则表达式

2023-09-09 21:48:08 作者:〓全民男神〓

使用ActionScript 3.0(在Flash CS5)

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.

必须有一些我无法找到在现场的文档,或在谷歌无痛苦的方式。请问AS3甚至认为这个功能?如果不是,它真的应该。

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?

推荐答案

其实我已经在博客中这一点,所以我就点你有:http://tyleregeto.com/using-vars-in-regular-ex$p$pssions-as3它谈到了可能的解决方案,但目前还没有理想的人像你提了。

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.

修改

下面是博客条目的重要组成部分的副本:

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

这漂亮的EX pression就像一个魅力。但我想更新它使开发人员可以限制哪些标签是剥夺那些在​​数组中指定。 pretty的直线前进的东西,用在正则表达式的变量值,你首先需要建立它作为一个字符串,然后将其转换。类似如下:

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的直线前进。因此,处理这个小的提升的时候,这就是我所做的。当然,一个大的问题是pretty的清晰。

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 + '("[^"]*"|'[^']*'|[^'">])*>/';

你猜怎么着,无效的字符串!更好地逃避字符串中的引号。哎呦,这将打破正则表达式!我被难住了。于是我打开了语言参考,看看我能找到。 源参数(这是我以前从来没有用过,)吸引了我的眼球。它返回描述为一个字符串的普通恩pression的模式部分。它完全没有的伎俩。这里是解决方案:

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');