任何人都知道为什么“x”.split(/(x)/)。长度在IE中返回0?

时间:2021-02-22 21:39:23

In IE, "x".split(/(x)/).length returns 0

在IE中,“x”.split(/(x)/)。length返回0

In Firefox, Chrome, Safari, and Opera, it returns 3.

在Firefox,Chrome,Safari和Opera中,它返回3。

Does anybody know the reason why? If possible, a reference link will be greatly appreciated.

有人知道原因吗?如果可能,将非常感谢参考链接。

I believe that it is a IE regex implementation issue, but I can't find any document about that.

我相信这是一个IE正则表达式实现问题,但我找不到任何关于它的文档。

3 个解决方案

#1


6  

You're correct that there are implementation issues. IE both ignores empty values and capture blocks within regular expressions.

你确实存在实施问题。 IE都忽略空值并捕获正则表达式中的块。

So for

因此对于

"foo".split(/o/)

IE gives

IE给出了

[f]

where the other browsers give

其他浏览器给出的位置

["f","",""]

and when you add the capturing:

当你添加捕获时:

"foo".split(/(o)/)

IE performs the same, but the others add the captured delimiter to the resulting array to give

IE执行相同的操作,但其他人将捕获的分隔符添加到结果数组中

["f","o","","o",""]

So unfortunately you probably either need to avoid using split, or code around these issues.

所以不幸的是,您可能要么避免使用拆分,要么围绕这些问题进行编码。

#2


3  

Here for example http://blog.stchur.com/2007/03/28/split-broken-in-ie/

例如http://blog.stchur.com/2007/03/28/split-broken-in-ie/

#3


1  

I had the same problem with the broken IE implementation of split.

我遇到了与拆分的IE实施相同的问题。

Here's a small library file that fixed the problem perfectly.

这是一个小型库文件,可以完美地解决问题。

#1


6  

You're correct that there are implementation issues. IE both ignores empty values and capture blocks within regular expressions.

你确实存在实施问题。 IE都忽略空值并捕获正则表达式中的块。

So for

因此对于

"foo".split(/o/)

IE gives

IE给出了

[f]

where the other browsers give

其他浏览器给出的位置

["f","",""]

and when you add the capturing:

当你添加捕获时:

"foo".split(/(o)/)

IE performs the same, but the others add the captured delimiter to the resulting array to give

IE执行相同的操作,但其他人将捕获的分隔符添加到结果数组中

["f","o","","o",""]

So unfortunately you probably either need to avoid using split, or code around these issues.

所以不幸的是,您可能要么避免使用拆分,要么围绕这些问题进行编码。

#2


3  

Here for example http://blog.stchur.com/2007/03/28/split-broken-in-ie/

例如http://blog.stchur.com/2007/03/28/split-broken-in-ie/

#3


1  

I had the same problem with the broken IE implementation of split.

我遇到了与拆分的IE实施相同的问题。

Here's a small library file that fixed the problem perfectly.

这是一个小型库文件,可以完美地解决问题。