简体   繁体   English

JavaScript split()仅在Firefox / IE中向数组添加额外的项

[英]JavaScript split() adding extra item to array in Firefox/IE only

I'm trying to understand the behaviour of different browsers with the JavaScript split() method, matching by linefeed (\\n) regexp. 我正在尝试通过JavaScript feed(\\ n)正则表达式匹配的JavaScript split()方法来了解不同浏览器的行为。

I have a textarea input form which is to take rows pasted from Excel and split them by linefeed into an array: 我有一个textarea输入表单,该表单接受从Excel粘贴的行,并通过换行将它们拆分成一个数组:

var rowsplit = document.getElementById("inputfield").value.split(/\n/g);

The user selects the rows in Excel by the side bar and so the first 11 columns have data and there are a whole bunch of empty columns after those to the edge of the spreadsheet that I slice() out later. 用户通过边栏选择Excel中的行,因此前11列具有数据,在电子表格的边缘之后有一大堆空列,我稍后将它们切片()。 So a typical input from Excel would be: 因此,Excel的典型输入为:

[data][data][data][data]x11[null][null][null]etc until edge of spreadsheet

The difference is what is matched by the regexp and pushed into the array. 区别在于正则表达式匹配并推入数组。

In both Chrome 28 and Safari 5.1, when selecting three rows of information, it correctly matches the \\n as 3, outputs them to the array and goes frolicking through a daisy field without a care in the world. 在Chrome 28和Safari 5.1中,当选择三行信息时,它正确地将\\ n匹配为3,然后将它们输出到数组中,然后遍历雏菊字段,而无需担心。

In IE10 and Firefox 19 though, it matches the first three linefeeds fine and then pulls an extra one which is blank. 不过,在IE10和Firefox 19中,它与前三个换行符匹配,然后再拉出一个空白。 So the \\n expression splits it into four items, crashes into a building and kills hundreds. 因此,\\ n表达式将其分为四部分 ,撞倒在建筑物中并杀死数百人。

I have read a lot about different browsers handling the CRLF characters differently and tried splitting by \\r\\n and just \\r but it seems to introduce more trouble. 我已经阅读了很多关于不同浏览器以不同方式处理CRLF字符的信息,并尝试通过\\ r \\ n和\\ r进行拆分,但这似乎带来了更多麻烦。 I also copied the Excel rows into Notepad++ and turned on visibility of the CRLF characters to see that there only appears to be 3, so why are IE and Firefox adding another one? 我也将Excel行复制到Notepad ++中,并打开了CRLF字符的可见性,看到只有3个字符,为什么IE和Firefox又添加了一个?

I've also read a lot about the pains people have with regexp, JS, browsers, line breaks AND Excel but most of it seems to be more relevant to older browsers so I was hoping someone with knowledge on the current state of things could provide some insight. 我还读了很多关于人们使用正则表达式,JS,浏览器,换行符和Excel的痛苦的信息,但大多数似乎与较旧的浏览器更相关,因此我希望了解当前情况的人可以提供一些见识。

Thanks! 谢谢!

Presumably, IE10 and Firefix 19 see the value as having an extra, trailing line-break. 大概IE10和Firefix 19认为该值具有额外的尾随换行符。 .split() won't ignore it just because it's at the end. .split()不会仅仅因为它在结尾而忽略它。

You should be able to remove it with .trim() before .split() 'ing: 您应该能够在.split()之前使用.trim()将其删除:

var rowsplit = document.getElementById("inputfield").value.trim().split(/\n/g);

Another option is to use a zero-width look-ahead to only match \\n that have anything after them: 另一种选择是使用零宽度预读仅匹配\\n后面有任何内容:

var rowsplit = document.getElementById("inputfield").value.split(/\n(?=\s*\S)/g);

Example of each: http://jsfiddle.net/FChES/ 每个示例: http : //jsfiddle.net/FChES/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM