这是 .split() 在 IE8 中的预期行为吗?
Is this expected behaviour for .split() in IE8?
我刚刚在 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 的错误,或者这是否是预期的,并且有更好的方法可以在不使用的情况下实现相同的结果需要修改分割方式
这是预期的(某种程度上)行为。
来自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 在生成另一个空字符串时逐字地采用字符串末尾的 'separator' 的概念)。
我刚刚在 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 的错误,或者这是否是预期的,并且有更好的方法可以在不使用的情况下实现相同的结果需要修改分割方式
这是预期的(某种程度上)行为。
来自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 在生成另一个空字符串时逐字地采用字符串末尾的 'separator' 的概念)。