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 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 ()
)
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 ()
)
本文标签: javascripte matches entire stringrather than expected substringStack Overflow
版权声明:本文标题:javascript - .?e.? matches entire string, rather than expected substring - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745575181a2156975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论