有人知道为什么"x" .split(/(x)/).length在IE中返回0吗? [英] Anybody know why "x".split(/(x)/).length returns 0 in IE?

查看:75
本文介绍了有人知道为什么"x" .split(/(x)/).length在IE中返回0吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆