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 incdef
instead of thedef
I stated in the question. I'll update the Fiddle to remove the inconsistency. – ruakh Commented Jul 28, 2013 at 6:52
3 Answers
Reset to default 6As 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 ()
)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745575183a4633920.html
评论列表(0条)