开发者

Javascript text divide by tabs problem (multiline-cells)

开发者 https://www.devze.com 2023-03-09 01:36 出处:网络
I have a strange issue in my Web Page, specifically with a text area element that obtains the clipboard from the user.

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:

Javascript text divide by tabs problem (multiline-cells)

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 = docum开发者_JAVA百科ent.getElementById("TextAreaElement").value;
var vArray = myData.split(/\n/);

But this array return me 3 lines...

Javascript text divide by tabs problem (multiline-cells)


Somebody knows any solution or alternative way?


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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消