4 Replies Latest reply on Feb 17, 2009 12:48 PM by VarioPegged

    How to find Mac line feed/return using split?

    kmdguy
      I have at text area where the user can paste in lines of text from an outside source. I'm then running a function that splits the textarea into individual items into an array so that each individual line becomes an item in the array. It all works fine but I'm running into one problem with Mac line endings. Here's my code:

      var split:String = textInput.text;
      var URLarray:Array = split.split(/\n|\r|\r\n|\n\r/g);

      If a person is on a PC and copies some text from Word or Notepad, this routine works fine and nicely creates the array containing however many items in it as there are lines in the textarea.

      On the mac (which is where I'm coding this), a person can manually type in the textarea many lines and the code still works fine. However, if on the mac a user copies/pastes a block of lines from any text editor, the code only recognizes all the lines as one continuous line with no line breaks.

      Is there a regular expression I can use in my code to parse the mac line breaks as I can't seem to find anything about it online.

      Thanks
        • 1. Re: How to find Mac line feed/return using split?
          kmdguy Level 1
          What I'm finding out by doing some tests, is that when something is copied and pasted from OSX into a textarea, that no line ending or carriage return characters even make it.

          I created this function:
          public function doTester():void
          {
          var str:String = TextInput2.text;
          for (var i:int = 0; i<str.length;i++)
          {
          trace(str.charAt(i), "-", str.charCodeAt(i));
          }
          }
          If type in 2 lines on the Mac I get the following acceptable trace result:
          L - 76
          i - 105
          n - 110
          e - 101
          1 - 49

          - 13
          L - 76
          i - 105
          n - 110
          e - 101
          2 - 50

          If I type in the exact same lines in any word processor on the Mac, and then copy/paste it into my textarea and perform the same function I get the following result:
          L - 76
          i - 105
          n - 110
          e - 101
          1 - 49
          L - 76
          i - 105
          n - 110
          e - 101
          2 - 50

          Notice how the copy/pasted version has no indication of any line ending between Line1 and Line2. This is not an issue at all on the PC. I need to be able to break apart each line from this textarea and I don't know if users will be using Macs or PCs so I need to be to figure out a solution in case someone is copying and pasting from a Mac. Anybody have any ideas on how to insert the line breaks in text that is copied/pasted from a Mac?

          Thanks
          • 2. How to find Mac line feed/return using split?
            VarioPegged Level 2
            I don't know if one might be overlooking something obvious, but I was intrigued by your problem.

            Using the "numLines" and "getLineText" properties of the TextField within the TextArea might be an acceptable workaround for you on the Mac. Note that it will discard empty lines.

            TS
            • 3. Re: How to find Mac line feed/return using split?
              kmdguy Level 1
              Thanks Vario,

              You are a genius! This solution was tested and indeed works. I didn't even know about the technique you described here. Brilliant work, you saved my butt.

              Thanks a million.
              • 4. Re: How to find Mac line feed/return using split?
                VarioPegged Level 2
                You're welcome. Glad that worked for you!

                TS