Javascript文本除以制表符问题(多行单元格)

发布于 2024-11-10 04:18:17 字数 619 浏览 3 评论 0原文

我的网页中有一个奇怪的问题,特别是从用户获取剪贴板的文本区域元素。

用户执行 CTRL+V;我创建了一个事件来通过事件 KeyUp 获取数据。

...这工作正常...

但是,当我尝试除以该文本区域的每个“行”时;开始问题... 输入可以像这样的示例:

excel

数据读取如下:

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 行...

debug


有人知道任何解决方案或者替代方式?

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:

excel

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...

debug


Somebody knows any solution or alternative way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧竹 2024-11-17 04:18:17

您将获得一个包含三行的文本,将其按换行符分割,并获得一个包含三个项目的数组。看起来好像有效。 :) 现在,您需要做的就是将这些项目中的每一项拆分到选项卡上 ( = \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.

伤感在游骋 2024-11-17 04:18:17

关于“.”的问题不匹配换行符,在 JS 中执行此操作的标准方法是 [\S\s] 它将匹配任何内容。

对于初学者来说,您想要做的似乎就是用制表符分割字符串,对吗?然后...

result = string.split(/\t/)

然后您将得到一个数组,其中每行的数据都是分开的。请注意,只有当您的数据中不能包含额外的错误选项卡时,这才有效。

无论使用什么工具将信息放入剪贴板,都应该在将其复制出来并由 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...

result = string.split(/\t/)

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.

你与清晨阳光 2024-11-17 04:18:17

好吧,我找不到使用某些正则表达式或 javascript 方法的方法(我相信可以做到)。我采用了不同的方式来分割信息。

我使用 AJAX 将此信息发送到服务器并在 VB 中执行拆分。
在简历中:

  1. 我得到最大列(按选项卡分割)。

  2. 获取并评估选项卡数组的每个值。

  3. 如果以双引号开头,尝试找到双引号的结尾(在此之前,用唯一的文本替换中间双引号)

  4. 每次评估原始数组的项目时,我都会删除每个项目(始终评估项目 0)...

  5. 如果找到新行(行的最后一行),则仅删除上一行“最后一列”的文本。

我希望对遇到同样问题的人有所帮助。干杯。

代码如下:

Public Function TEST(ByVal pText As String) As String
    Try
        Dim vText As String = pText

        Dim vArray As New ArrayList
        vArray.AddRange(vText.Split(vbNewLine))
        Dim vActualIndex As Integer = 0

        Dim vMaxColumns As Integer = 0

        For Each vArrayItem In vArray
            If vArrayItem.Split(vbTab).Length > vMaxColumns Then
                vMaxColumns = vArrayItem.Split(vbTab).Length
            End If
        Next

        Dim vActualArray(vMaxColumns - 1) As String
        vArray = New ArrayList
        vArray.AddRange(vText.Split(vbTab))

        Dim vLen As Integer = vArray.Count
        Dim vNewArray As New ArrayList

        vActualIndex = 0

        Do While vArray.Count <> 0
            If vArray(0).Split(vbNewLine).Length = 1 Then
                vActualArray(vActualIndex) = vArray(0)
                vActualIndex += 1
            Else
                If vArray(0).Split(vbNewLine)(0).ToString.StartsWith("""") Then
                    vArray(0) = Mid(vArray(0), 2).Replace("""""", "*_IDIDIDUNIQUEID_*")

                    If InStr(vArray(0), """" & vbNewLine) <> 0 Then
                        vActualArray(vActualIndex) = Mid(vArray(0), 1, InStr(vArray(0), """" & vbNewLine) + 1)
                        vArray(0) = Mid(vArray(0), InStr(vArray(0), """" & vbNewLine) + 3)

                        vActualArray(vActualIndex) = vActualArray(vActualIndex).ToString.Replace("*_IDIDIDUNIQUEID_*", """""")
                        vArray(0) = vArray(0).ToString.Replace("*_IDIDIDUNIQUEID_*", """""")

                        vActualIndex += 1

                        GoTo Skip_remove
                    End If

                    vArray(0) = vArray(0).ToString.Replace("*_IDIDIDUNIQUEID_*", """""")
                    vActualArray(vActualIndex) = vArray(0)
                    vActualIndex += 1
                Else
                    vActualArray(vActualIndex) = vArray(0).Split(vbNewLine)(0)
                    vActualIndex += 1

                    vArray(0) = vArray(0).ToString.Substring(vArray(0).Split(vbNewLine)(0).ToString.Length + 2)
                    GoTo Skip_remove
                End If
            End If

            vArray.RemoveAt(0)

'这是 VB 代码中的标签
跳过_删除:

            If vActualIndex >= vMaxColumns Then
                vNewArray.Add(vActualArray)

                ReDim vActualArray(vMaxColumns - 1)

                vActualIndex = 0
            End If
        Loop
    Catch ex As Exception
        Return ""
    End Try
End Function

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:

  1. I get the max columns (split by tabs).

  2. Get and evaluate each value of the array of tabs.

  3. 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)

  4. Every time that evaluated an item of the original Array, I deleted each item (Always evaluate the item 0)...

  5. 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:

Public Function TEST(ByVal pText As String) As String
    Try
        Dim vText As String = pText

        Dim vArray As New ArrayList
        vArray.AddRange(vText.Split(vbNewLine))
        Dim vActualIndex As Integer = 0

        Dim vMaxColumns As Integer = 0

        For Each vArrayItem In vArray
            If vArrayItem.Split(vbTab).Length > vMaxColumns Then
                vMaxColumns = vArrayItem.Split(vbTab).Length
            End If
        Next

        Dim vActualArray(vMaxColumns - 1) As String
        vArray = New ArrayList
        vArray.AddRange(vText.Split(vbTab))

        Dim vLen As Integer = vArray.Count
        Dim vNewArray As New ArrayList

        vActualIndex = 0

        Do While vArray.Count <> 0
            If vArray(0).Split(vbNewLine).Length = 1 Then
                vActualArray(vActualIndex) = vArray(0)
                vActualIndex += 1
            Else
                If vArray(0).Split(vbNewLine)(0).ToString.StartsWith("""") Then
                    vArray(0) = Mid(vArray(0), 2).Replace("""""", "*_IDIDIDUNIQUEID_*")

                    If InStr(vArray(0), """" & vbNewLine) <> 0 Then
                        vActualArray(vActualIndex) = Mid(vArray(0), 1, InStr(vArray(0), """" & vbNewLine) + 1)
                        vArray(0) = Mid(vArray(0), InStr(vArray(0), """" & vbNewLine) + 3)

                        vActualArray(vActualIndex) = vActualArray(vActualIndex).ToString.Replace("*_IDIDIDUNIQUEID_*", """""")
                        vArray(0) = vArray(0).ToString.Replace("*_IDIDIDUNIQUEID_*", """""")

                        vActualIndex += 1

                        GoTo Skip_remove
                    End If

                    vArray(0) = vArray(0).ToString.Replace("*_IDIDIDUNIQUEID_*", """""")
                    vActualArray(vActualIndex) = vArray(0)
                    vActualIndex += 1
                Else
                    vActualArray(vActualIndex) = vArray(0).Split(vbNewLine)(0)
                    vActualIndex += 1

                    vArray(0) = vArray(0).ToString.Substring(vArray(0).Split(vbNewLine)(0).ToString.Length + 2)
                    GoTo Skip_remove
                End If
            End If

            vArray.RemoveAt(0)

' This is a label in VB code
Skip_remove:

            If vActualIndex >= vMaxColumns Then
                vNewArray.Add(vActualArray)

                ReDim vActualArray(vMaxColumns - 1)

                vActualIndex = 0
            End If
        Loop
    Catch ex As Exception
        Return ""
    End Try
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文