Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

/.?e.?/ matches entire string, rather than expected substring

In Internet Explorer 10, this:

'abcdefghi'.match(/.?e.?/)

evaluates to ['def'], as I'd expect, but in Firefox 21.0, it evaluates to ['abcdefghi']. (See this jsFiddle.) I get the same sort of unexpected behavior for certain other regexes that both begin and end with optional content, such as /.?e.{0,2}/ and /.{0,2}e.{0,2}/; however, commenters point out various similar regexes, such as /\S?e\S?/ and /(?:.?e.?)/, that are not affected. The same applies to the replace method.

Am I missing something obvious? Is there some deep reason for this behavior?

like image 320
ruakh Avatar asked Jul 28 '13 06:07

ruakh


2 Answers

As tiffon said, this is a bug in SpiderMonkey (Firefox's JavaScript engine).

In SpiderMonkey, we use the RegExp engine from Safari's JavaScriptCore JS engine, and inherited the bug from that. I filed bug 119191 for the bug in JSC.

like image 116
Till Schneidereit Avatar answered Oct 17 '22 16:10

Till Schneidereit


Looks like a bug. I filed an issue.

Btw, the following work fine:

'abcdefghi'.match(/.e./)
'abcdefghi'.match(/.e.?/)
'abcdefghi'.match(/.?e./)
'abcdefghi'.match(/[a-z]?e.?/)
'abcdefghi'.match(/.?e[a-z]?/)

http://jsfiddle.net/afDqC/1/

like image 2
tiffon Avatar answered Oct 17 '22 16:10

tiffon