'undefined' matches in AS3 RegExp? Yes?

I have a long regular expression in AS3 / Flex that finds one of several dozen words. The regex looks like this: word | wordup | wordly | wordster

when I do "wordup wordster!". match (regex) I get undefined swings! returned matches array Matches: [0] 'wordup' [1] undefined array length: 2

Is there a known bug in AS3 grouping matches? What can a few words do in the returned array of matches while others are returned as undefind?

I looked for the wrong characters in my regex and checked the regex multiple times.

If I just search for "wordup" then I get an array of matches of length 1 with the correct content. If I only search for "wordster" I get an array of length one with matches [0], being undefined again.

------ update -------

After a lot of experimenting ... my regex was too long for AS3 My actual regex used grouping and had an optional parenthesis:

(?: (? (\ bword \ b))? | (? (\ bwordup \ b))? | ... etc for 51 words.

simplification:? (: \ Bword \ b | \ bwordup \ b |

somehow made the matching groups work fine, although I don't have the parentheses that are usually needed to define the groups ...

0


source to share


1 answer


When dealing with "mystery" problems, you should always show your actual code, not what you think is equivalent. word | wordup | wordly | wordster won't give you any "undefined" matches.

Instead of using (?: \ Bword \ b | \ bword2 \ b) use this: \ b (?: Word | word2) \ b



Regular Expression Word | (word2)? | word3 will give you zero-length matches as the second alternative in the regex is optional. It will match a zero-length string at every position in the string where "word" cannot be matched.

+3


source







All Articles