Javascript文本除以制表符问题(多行单元格)
我的网页中有一个奇怪的问题,特别是从用户获取剪贴板的文本区域元素。
用户执行 CTRL+V;我创建了一个事件来通过事件 KeyUp 获取数据。
...这工作正常...
但是,当我尝试除以该文本区域的每个“行”时;开始问题... 输入可以像这样的示例:
数据读取如下:
Row1[0][HT]Row1[1 ][LF]"Row2[0] 注释行 1[LF]Row2[0] 注释行 2"[HT]Row2[1]
其中: [HT] 表示 {Tab} [LF] 表示
我使用的 {New line}:
var myData = document.getElementById("TextAreaElement").value;
var vArray = myData.split(/\n/);
但是这个数组返回了 3 行...
有人知道任何解决方案或者替代方式?
I have a strange issue in my Web Page, specifically with a text area element that obtains the clipboard from the user.
The user perform a CTRL+V; and I created a event to get the data with the event KeyUp.
...this works fine...
But, when I try to divide by each "row" of this textarea; start the problems...
The input can be like this example:
The data reads something like that:
Row1[0][HT]Row1[1][LF]"Row2[0] Comment line 1[LF]Row2[0] Comment line 2"[HT]Row2[1]
Where:
[HT] means {Tab}
[LF] means {New line}
I use:
var myData = document.getElementById("TextAreaElement").value;
var vArray = myData.split(/\n/);
But this array return me 3 lines...
Somebody knows any solution or alternative way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将获得一个包含三行的文本,将其按换行符分割,并获得一个包含三个项目的数组。看起来好像有效。 :) 现在,您需要做的就是将这些项目中的每一项拆分到选项卡上 ( =
\t
)[编辑]
不,现在我明白您的意思了。通过这种分裂你不会到达那里。您必须解析该字符串。字段值可以包含回车符,在这种情况下,它将用双引号引起来。因此,您必须解析该字符串,并且当您仍在一组引号内时,不要在中断时将其拆分。
You get a text containing three lines, split it on the line breaks and get an array with three items. Seems like it works. :) Now, what you need to do, is take each of these items and split them on a tab ( =
\t
)[edit]
Nope, now I see what you mean. You won't get there by using this splitting. You'll have to parse the string. A field value can contain an enter, in which case it will be enclosed in double quotes. So you'll have to parse the string and don't split it on a break when you're still within a set of quotes.
关于“.”的问题不匹配换行符,在 JS 中执行此操作的标准方法是
[\S\s]
它将匹配任何内容。对于初学者来说,您想要做的似乎就是用制表符分割字符串,对吗?然后...
然后您将得到一个数组,其中每行的数据都是分开的。请注意,只有当您的数据中不能包含额外的错误选项卡时,这才有效。
无论使用什么工具将信息放入剪贴板,都应该在将其复制出来并由 JS 解析之前进行一些转义。如果它不能做到这一点,那么实际上任何事情都会发生 - 在这种情况下,您无法保证您的字符串不会有双引号、制表符或您可能尝试用作分隔符的任何其他字符。
Regarding the problem of '.' not matching newlines, the standard method of doing that in JS is
[\S\s]
which will match anything.It looks like all you want to do for starters is split the string by tabs, right? Then...
Then you'll have an array with each of the rows' data separate. Note that this only works if your data can't have extra erroneous tabs in it.
Whatever tool is getting the information into the clipboard should really do some escaping before it is copied out and parsed by your JS. If it can't do that, then really anything goes - you can't in that case guarantee that your string won't have double-quotes, tabs, or any other character you might try to use as a delimiter.
好吧,我找不到使用某些正则表达式或 javascript 方法的方法(我相信可以做到)。我采用了不同的方式来分割信息。
我使用 AJAX 将此信息发送到服务器并在 VB 中执行拆分。
在简历中:
我得到最大列(按选项卡分割)。
获取并评估选项卡数组的每个值。
如果以双引号开头,尝试找到双引号的结尾(在此之前,用唯一的文本替换中间双引号)
每次评估原始数组的项目时,我都会删除每个项目(始终评估项目 0)...
如果找到新行(行的最后一行),则仅删除上一行“最后一列”的文本。
我希望对遇到同样问题的人有所帮助。干杯。
代码如下:
'这是 VB 代码中的标签
跳过_删除:
Well, I don't find the way to work with some regular expression or a javascript method (I believe that can do it). I worked a different way to split the info.
I used AJAX to send this information to the server and perform the split in VB.
In resume:
I get the max columns (split by tabs).
Get and evaluate each value of the array of tabs.
If start with double quotes, tried to find the end of the double quotes (before that, replaced the mid double quotes with a unique text)
Every time that evaluated an item of the original Array, I deleted each item (Always evaluate the item 0)...
If find a new line (final of the row), only removed the text of the "final column" of the previous row.
I hope help to someone with the same problem. Cheers.
Here is the code:
' This is a label in VB code
Skip_remove: