PDA

View Full Version : Regex Unexpected Quantifier


Philip M
04-18-2003, 11:24 AM
This is a follow up to

http://www.codingforums.com/showthread.php?s=&threadid=18252 (Occasional failure of promptbox)

I have extracted and condensed the following from another forum.

--------------------------------------------

Regular Expressions
Unexpected Quantifier when non-greedy

var re = new RegExp("(.*object>" + sObj + ".*?selected>)false(<\/selected.*)","i")

Causes errors on some installs of IE and not on others: "unexpected quantifier". It's fine on about 80% of the machines in the office but the remaining 20% seem to not like the '?' non-greedy* operator. The weird thing is that the failure doesn't stick to any one version of IE - it's happened on some 5.0 and some 5.5 machines whilst working on most of them.

It seems to work fine Netscape 7 too.

From JScript regular expression syntax guide:?When this character immediately follows
any of the other quantifiers (*, +,?, {n}, {n,}, {n,m}), the matching pattern is non-greedy....

It turns out that it's all down to the Windows Scripting Engine Version (for VBScript and JScript) - Version 5.1 and below doesn't seem to support non-greedy patterns whereas 5.6 is fine.

-------------------------------------------------------

Note that the problem arises in IE5.0 all the time and occasionally in IE 5.5 - this was exactly the symptoms I experienced.

In my form the PREVIOUS textbox entry was valided by a script containing a regex including ? (non-greedy matching)
If this failed it also screwed up another script which validated
the NEXT textbox entry (but not it seems any subsequent textbox
validations).

I think it worthwhile to post this for general information.
The problem is exceedingly subtle and indeed baffling and weird.
Moral - avoid non-greedy pattern matching!

Thank you noble Beetle for pointing me in this direction. You were right, as usual.

But I feel that I really have solved the &^%£?@£$ thing now!
I would add that I have spent I guess 60 hours on this one!