有人知道为什么"x" .split(/(x)/).length在IE中返回0吗? [英] Anybody know why "x".split(/(x)/).length returns 0 in IE?
问题描述
在IE中,"x".split(/(x)/).length
返回0
在Firefox,Chrome,Safari和Opera中,它返回3
.
In Firefox, Chrome, Safari, and Opera, it returns 3
.
有人知道原因吗?如果可能的话,将不胜感激参考链接.
Does anybody know the reason why? If possible, a reference link will be greatly appreciated.
我认为这是IE regex实现的问题,但是我找不到有关此问题的任何文档.
I believe that it is a IE regex implementation issue, but I can't find any document about that.
推荐答案
您是正确的,存在实现问题. IE会忽略空值并捕获正则表达式中的块.
You're correct that there are implementation issues. IE both ignores empty values and capture blocks within regular expressions.
所以
"foo".split(/o/)
IE提供
[f]
其他浏览器提供的地方
["f","",""]
以及添加捕获时:
"foo".split(/(o)/)
IE执行相同的操作,但是其他人将捕获的定界符添加到结果数组中以给出
IE performs the same, but the others add the captured delimiter to the resulting array to give
["f","o","","o",""]
不幸的是,您可能需要避免使用split或对这些问题进行编码.
So unfortunately you probably either need to avoid using split, or code around these issues.
这篇关于有人知道为什么"x" .split(/(x)/).length在IE中返回0吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!