JavaScript string newline character?

后端 未结 15 1900
终归单人心
终归单人心 2020-11-22 08:06

Is \\n the universal newline character sequence in Javascript for all platforms? If not, how do I determine the character for the current environment?

I

相关标签:
15条回答
  • 2020-11-22 08:29
    printAccountSummary: function()
            {return "Welcome!" + "\n" + "Your balance is currently $1000 and your interest rate is 1%."}
    };
    console.log(savingsAccount.printAccountSummary()); // method
    

    Prints:

    Welcome!
    Your balance is currently $1000 and your interest rate is 1%.
    
    0 讨论(0)
  • 2020-11-22 08:36

    A practical observation... In my NodeJS script I have the following function:

    function writeToLogFile (message) {
        fs.appendFile('myserverlog.txt', Date() + " " + message + "\r\n", function (err) {
            if (err) throw err;
        });
    }
    

    First I had only "\n" but I noticed that when I open the log file in Notepad, it shows all entries on the same line. Notepad++ on the other hand shows the entries each on their own line. After changing the code to "\r\n", even Notepad shows every entry on its own line.

    0 讨论(0)
  • 2020-11-22 08:36

    The \n is just fine for all cases I've encountered. I you are working with web, use \n and don't worry about it (unless you have had any newline-related issues).

    0 讨论(0)
  • 2020-11-22 08:37

    I've just tested a few browsers using this silly bit of JavaScript:

    function log_newline(msg, test_value) {
      if (!test_value) { 
        test_value = document.getElementById('test').value;
      }
      console.log(msg + ': ' + (test_value.match(/\r/) ? 'CR' : '')
                  + ' ' + (test_value.match(/\n/) ? 'LF' : ''));
    }
    
    log_newline('HTML source');
    log_newline('JS string', "foo\nbar");
    log_newline('JS template literal', `bar
    baz`);
    <textarea id="test" name="test">
    
    </textarea>

    IE8 and Opera 9 on Windows use \r\n. All the other browsers I tested (Safari 4 and Firefox 3.5 on Windows, and Firefox 3.0 on Linux) use \n. They can all handle \n just fine when setting the value, though IE and Opera will convert that back to \r\n again internally. There's a SitePoint article with some more details called Line endings in Javascript.

    Note also that this is independent of the actual line endings in the HTML file itself (both \n and \r\n give the same results).

    When submitting a form, all browsers canonicalize newlines to %0D%0A in URL encoding. To see that, load e.g. data:text/html,<form><textarea name="foo">foo%0abar</textarea><input type="submit"></form> and press the submit button. (Some browsers block the load of the submitted page, but you can see the URL-encoded form values in the console.)

    I don't think you really need to do much of any determining, though. If you just want to split the text on newlines, you could do something like this:

    lines = foo.value.split(/\r\n|\r|\n/g);
    
    0 讨论(0)
  • 2020-11-22 08:40

    Don't use "\n". Just try this:

    var string = "this\
    is a multi\
    line\
    string";
    

    Just enter a back-slash and keep on truckin'! Works like a charm.

    0 讨论(0)
  • 2020-11-22 08:41

    You can use `` quotes (wich are below Esc button) with ES6. So you can write something like this:

    var text = `fjskdfjslfjsl
    skfjslfkjsldfjslfjs
    jfsfkjslfsljs`;
    
    0 讨论(0)
提交回复
热议问题