xchg.ca
xchg.ca

Reputation: 1164

Is it possible to create wrong Regular expression in ActionScript/Flex which will cause runtime error?

Is it possible to create wrong Regular expression in ActionScript/Flex which will cause runtime error? I've tried so many weird regexpes in Flex and Flex never complained! How do I know If my regexp valid?

Upvotes: 2

Views: 1482

Answers (3)

darronschall
darronschall

Reputation: 859

In theory, according to the ActionScript 3.0 SyntaxError documentation, when a regular expression cannot be parsed a SyntaxError is generated at runtime that you can detect in a try/catch block.

In practice, I've never actually seen the RegExp class exhibit this behavior.

Upvotes: 3

DTRx
DTRx

Reputation: 251

If your end goal is to determine whether a particular regular expression is valid or not then I'm not sure trying to intentionally generate runtime errors is the best way to accomplish that.

Instead I would recommend testing your patterns against known inputs and make sure they behave as intended. You can use a tool like this to test: RegExr

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336178

I don't have ActionScript/Flex, so I can't test this. Since you haven't given any examples, I don't know what you think is a "weird" regex. What happens if you try one of these:

/(?<=x*)foo/

(ECMAScript regexes don't support lookbehind)

/foo([/

(missing closing parentheses/brackets)

/foo)]/

(missing opening parentheses/brackets)

/foo(?)/

(Syntax error)

/foo\1/

(invalid backreference)

Upvotes: 1

Related Questions