gpt4 book ai didi

javascript - 这是 IE8 中 .split() 的预期行为吗?

转载 作者:行者123 更新时间:2023-11-28 00:20:28 25 4
gpt4 key购买 nike

我刚刚在 IE8 中使用 .split() 方法时遇到了一个问题。

element.name = 'question[10]';
var splitName = element.name.split(/\[([0-9]+)\]/);
// Split name just had 1 element,
// splitName[0] == 'question'
// In Chrome/Firefox it has 3
// splitName[0] == 'question'
// splitName[1] == '10'
// splitName[2] == ''

我找到了使用 this code 的解决方法,但我很好奇这是否是 IE8 的一个错误,或者这是否是预期的,并且有更好的方法来实现相同的结果而不需要修改 split 方法。

最佳答案

这是预期的(某种程度上)行为。

来自MDN :

If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability.)

从正则表达式中删除括号,结果几乎相同(Chrome/FF 在生成另一个空字符串时逐字地在字符串末尾采用“分隔符”的概念)。

关于javascript - 这是 IE8 中 .split() 的预期行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30079770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com