How in node to split string by newline ('\n')?

后端 未结 7 1821
南笙
南笙 2020-11-29 17:30

How in node to split string by newline (\'\\n\') ? I have simple string like var a = \"test.js\\nagain.js\" and I need to get [\"test.js\", \"again.js\"]<

相关标签:
7条回答
  • 2020-11-29 18:11

    If the file is native to your system (certainly no guarantees of that), then Node can help you out:

    var os = require('os');
    
    a.split(os.EOL);
    

    This is usually more useful for constructing output strings from Node though, for platform portability.

    0 讨论(0)
  • 2020-11-29 18:19

    Try splitting on a regex like /\r?\n/ to be usable by both Windows and UNIX systems.

    > "a\nb\r\nc".split(/\r?\n/)
    [ 'a', 'b', 'c' ]
    
    0 讨论(0)
  • 2020-11-29 18:19

    It looks like regex /\r\n|\r|\n/ handles CR, LF, and CRLF line endings, their mixed sequences, and keeps all the empty lines inbetween. Try that!

    function splitLines(t) { return t.split(/\r\n|\r|\n/); }
    
    // single newlines
    splitLines("AAA\rBBB\nCCC\r\nDDD");
    // double newlines
    splitLines("EEE\r\rFFF\n\nGGG\r\n\r\nHHH");
    // mixed sequences
    splitLines("III\n\r\nJJJ\r\r\nKKK\r\n\nLLL\r\n\rMMM");
    

    You should get these arrays as a result:

    [ "AAA", "BBB", "CCC", "DDD" ]
    [ "EEE", "", "FFF", "", "GGG", "", "HHH" ]
    [ "III", "", "JJJ", "", "KKK", "", "LLL", "", "MMM" ]
    

    You can also teach that regex to recognize other legit Unicode line terminators by adding |\xHH or |\uHHHH parts, where H's are hexadecimal digits of the additional terminator character codepoint (as seen in Wikipedia article as U+HHHH).

    0 讨论(0)
  • 2020-11-29 18:23

    The first one should work:

    > "a\nb".split("\n");
    [ 'a', 'b' ]
    > var a = "test.js\nagain.js"
    undefined
    > a.split("\n");
    [ 'test.js', 'again.js' ]
    
    0 讨论(0)
  • 2020-11-29 18:24

    A solution that works with all possible line endings including mixed ones and keeping empty lines as well can be achieved using two replaces and one split as follows

    text.replace(/\r\n/g, "\r").replace(/\n/g, "\r").split(/\r/);
    

    some code to test it

      var CR = "\x0D";  //   \r
      var LF = "\x0A";  //   \n
    
      var mixedfile = "00" + CR + LF +            // 1 x win
                      "01" + LF +                 // 1 x linux
                      "02" + CR +                 // 1 x old mac
                      "03" + CR + CR +            // 2 x old mac
                      "05" + LF + LF +            // 2 x linux
                      "07" + CR + LF + CR + LF +  // 2 x win
                      "09";
    
      function showarr (desc, arr)
      {
         console.log ("// ----- " + desc);
         for (var ii in arr)
            console.log (ii + ") [" + arr[ii] +  "] (len = " + arr[ii].length + ")");
      }
    
      showarr ("using 2 replace + 1 split", 
               mixedfile.replace(/\r\n/g, "\r").replace(/\n/g, "\r").split(/\r/));
    

    and the output

      // ----- using 2 replace + 1 split
      0) [00] (len = 2)
      1) [01] (len = 2)
      2) [02] (len = 2)
      3) [03] (len = 2)
      4) [] (len = 0)
      5) [05] (len = 2)
      6) [] (len = 0)
      7) [07] (len = 2)
      8) [] (len = 0)
      9) [09] (len = 2)
    
    0 讨论(0)
  • 2020-11-29 18:28
    a = a.split("\n");
    

    Note that splitting returns the new array, rather than just assigning it to the original string. You need to explicitly store it in a variable.

    0 讨论(0)
提交回复
热议问题