pr said:
Thomas said:
As for the matching quotes, you should use
('foo'|"foo")
Or
(['"])foo\1
Correct. To my surprise, this feature, standardized only with ECMAScript
Ed. 3 (like regular expressions in general), appears to be widely supported:
The bookmarklet
javascript:window.alert(/^(["'])a\1b$/.test("'a'b"));
shows `true' in all my test environments, which currently are:
- Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.1)
Gecko/2008070208 Firefox/3.0.1
- Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14)
Gecko/20080404 Firefox/2.0.0.14
- Mozilla/4.78 [de] (Windows NT 5.0; U)
- Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE)
AppleWebKit/525.19 (KHTML, like Gecko) Version/3.1.2 Safari/525.21
- Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; de-de)
AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.22
- Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; {...};
.NET CLR 1.1.4322; .NET CLR 2.0.50727) (IE 8 beta 1)
- Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; {...};
.NET CLR 1.1.4322; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; {...};
.NET CLR 1.1.4322; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1; {...};
.NET CLR 1.1.4322; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0;
.NET CLR 1.1.4322; .NET CLR 2.0.50727)
- Mozilla/4.0 (compatible; MSIE 4.01; Windows NT 5.0; {...})
- Opera/9.52 (Windows NT 5.1; U; de)
- Opera/9.51 (Windows NT 5.1; U; de)
- Opera/9.27 (Windows NT 5.1; U; en)
- Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.0
- Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.1)
Opera 7.02 [en]
However, RegExp literals and non-greedy matching (`.*?') are not universally
supported, with the latter being the more important fact here.
Does this seem a reasonable feature test to you?
var ngq = /.+?/.exec("ab");
var hasNonGreedyQuantifiers = ngq && ngq[0].length == 1;
No, it could already throw a (non-catchable) SyntaxError when /.+?/ is
parsed, before execution (you can test that with IE 5.0, for example). And
I have yet to devise a bullet-proof test for possibly unsupported syntax (a
more sophisticated application of eval() comes to mind), one that does not
break the ECMAScript program then.
However,
var ngq = null;
try
{
ngq = new RegExp(".+?");
}
catch (e)
{
}
if (nqg)
{
// ...
}
would work for script engines that support basic exception handling but not
non-greedy quantifiers (such as JScript 5.1 in IE 5.01; tested positive).
PointedEars