1

这段代码从 Firefox 到 IE8 带来了不同的结果:

var text = "[AMOUNT] MILES DEDUCTED IN [CABIN] FOR [ORIGIN]";
var splitArray = text.split(/[\[\]]/);
alert(splitArray.length);   

在 Firefox 中,警报给出 7。而在 IE 中,它给出 5。

我试过...

Internet Explorer 中的 Javascript RegEx 问题

但它不起作用。

请问有什么想法吗?谢谢!

4

1 回答 1

0

下一个代码没有经过全面测试,但在快速测试中它可以正常工作:

要获得 IE8 和更少的拆分行为:

var text = "[AMOUNT] MILES DEDUCTED IN [CABIN] FOR [ORIGIN]";
var arr = text.match(/[^\[\]]+/g);
// arr is ["AMOUNT", " MILES DEDUCTED IN ", "CABIN", " FOR ", "ORIGIN"]

要获得 FF 拆分行为:

var text = "[AMOUNT] MILES DEDUCTED IN [CABIN] FOR [ORIGIN]";
var arr = text.match(/[^\[\]]+|^(?=[\[\]])|[^\[\]]?(?=[\[\]]$)|[^\[\]]?(?=[\[\]]{2})/g);
// arr is ["", "AMOUNT", " MILES DEDUCTED IN ", "CABIN", " FOR ", "ORIGIN", ""]
于 2012-08-24T16:04:28.957 回答