4

I'm new to actionscript, working on avm2..

One thing that I want to know is how to determine if a given regular expression is compiled or not in RegExp Class which use pcre library internally as regular expression compiler.

For example, the following has parenthesis-unmatched regular expression, which may be not compiled in pcre in RegExp class.

var r:RegExp = new RegExp("(a))");

I tried to use try-catch like the following, no exception occured.

try
{
    var r:RegExp = new RegExp("(a))");
} catch (e:Error) {
    trace('error');
}

I also tried to find solution on the Internet, there seem to be no method or properties for it.

Thanks.

hellotony
  • 41
  • 1
  • I also tried the exec and test methods. Very strange that no errors would be thrown. If AS3 RegExp supports recursion, this may solve your problem: http://stackoverflow.com/a/172316/2788187 – Connor Clark Aug 13 '15 at 01:59
  • Great question, I just tried typing it as a literal: `var r:RegExp = /(a))/;` and hoped for a compiler error but there wasn't any. If you have something that should match then you could "test" against it. – karfau Aug 17 '15 at 21:37
  • You could use a regex to test if its a regex :) http://stackoverflow.com/questions/172303/is-there-a-regular-expression-to-detect-a-valid-regular-expression AFAIK this is the best solution in AS3, since it does not tell you if a regex is not valid. – sydd Aug 26 '15 at 01:39
  • @sydd, that `RegExp` does not work in `AS3`. If you test "(\\)" it says that is `true`. – ElChiniNet Jan 05 '16 at 15:05
  • And if you test "(?:\?\:)" it says that is `false`. – ElChiniNet Jan 05 '16 at 15:08

1 Answers1

0

According to the documentation, an error in the regular expression will throw a SyntaxError:

A SyntaxError exception is thrown when a parsing error occurs, for one of the following reasons:.

    • An invalid regular expression is parsed by the RegExp class.

    • Invalid XML content is parsed by the XML class.

As a subclass of Error, your code should have caught it, but it appears broken in my testing.

miken32
  • 42,008
  • 16
  • 111
  • 154