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?
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With