admin管理员组

文章数量:1022864

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, menters 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?

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, menters 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?

Share Improve this question edited Jul 28, 2013 at 6:53 ruakh asked Jul 28, 2013 at 6:30 ruakhruakh 184k29 gold badges291 silver badges323 bronze badges 6
  • Well in chrome its: e de ef cdef +1 for this one. – Burhan Khalid Commented Jul 28, 2013 at 6:36
  • 1 Well, adding a group seems to correct it: 'abcdefghi'.match(/(?:.?e.?)/). But, I'm guessing bug. – Jonathan Lonowski Commented Jul 28, 2013 at 6:36
  • Same issue shows up in Firefox 22, but it should be noted that /.?.?/ works properly, as does /e.?/ and /.?e/ – SheetJS Commented Jul 28, 2013 at 6:47
  • 2 @BurhanKhalid, Which version of chrome do you use? I get ['def'] in chrome 28.0.1500.72. – falsetru Commented Jul 28, 2013 at 6:50
  • 1 @falsetru: Burhan Khalid is referring to the results of my Fiddle, which has .{0,2}e.? instead of .?e.?, resulting in cdef instead of the def I stated in the question. I'll update the Fiddle to remove the inconsistency. – ruakh Commented Jul 28, 2013 at 6:52
 |  Show 1 more ment

3 Answers 3

Reset to default 6

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/afDqC/1/

As the other answers stated, it appears to be a bug.

However, there's an easy workaround available: 'abcdefghi'.match(/(.?e.?)/)

That way you get correct results in both [0] (the implicit subgroup containing the whole string the regex matched) and [1] (the subgroup specified by ())

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, menters 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?

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, menters 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?

Share Improve this question edited Jul 28, 2013 at 6:53 ruakh asked Jul 28, 2013 at 6:30 ruakhruakh 184k29 gold badges291 silver badges323 bronze badges 6
  • Well in chrome its: e de ef cdef +1 for this one. – Burhan Khalid Commented Jul 28, 2013 at 6:36
  • 1 Well, adding a group seems to correct it: 'abcdefghi'.match(/(?:.?e.?)/). But, I'm guessing bug. – Jonathan Lonowski Commented Jul 28, 2013 at 6:36
  • Same issue shows up in Firefox 22, but it should be noted that /.?.?/ works properly, as does /e.?/ and /.?e/ – SheetJS Commented Jul 28, 2013 at 6:47
  • 2 @BurhanKhalid, Which version of chrome do you use? I get ['def'] in chrome 28.0.1500.72. – falsetru Commented Jul 28, 2013 at 6:50
  • 1 @falsetru: Burhan Khalid is referring to the results of my Fiddle, which has .{0,2}e.? instead of .?e.?, resulting in cdef instead of the def I stated in the question. I'll update the Fiddle to remove the inconsistency. – ruakh Commented Jul 28, 2013 at 6:52
 |  Show 1 more ment

3 Answers 3

Reset to default 6

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/afDqC/1/

As the other answers stated, it appears to be a bug.

However, there's an easy workaround available: 'abcdefghi'.match(/(.?e.?)/)

That way you get correct results in both [0] (the implicit subgroup containing the whole string the regex matched) and [1] (the subgroup specified by ())

本文标签: javascripte matches entire stringrather than expected substringStack Overflow