Asked  7 Months ago    Answers:  5   Viewed   63 times

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'm not asking about the HTML newline element (<BR/>). I'm asking about the newline character sequence used within JavaScript strings.

 Answers

55

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', "foonbar");
log_newline('JS template literal', `bar
baz`);
<textarea id="test" name="test">

</textarea>

IE8 and Opera 9 on Windows use rn. 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 rn 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 rn 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(/rn|r|n/g);
Tuesday, June 1, 2021
 
Ticksy
answered 7 Months ago
58

I would make use of the Environment.NewLine property.

Something like:

StringBuilder sb = new StringBuilder();
sb.AppendFormat("Foo{0}Bar", Environment.NewLine);
string s = sb.ToString();

Or

StringBuilder sb = new StringBuilder();
sb.Append("Foo");
sb.Append("Foo2");
sb.Append(Environment.NewLine);
sb.Append("Bar");
string s = sb.ToString();

If you wish to have a new line after each append, you can have a look at Ben Voigt's answer.

Tuesday, August 3, 2021
 
xenon
answered 4 Months ago
66

Editing my post:

Experimenting a bit, I find that sel.toString() returns new lines in contenteditable divs, while range.toString() returns newlines correctly in normal non-editable divs, but not in editable ones, as you reported.

Could not find any explanation for the behaviour though.

This is a useful link http://www.quirksmode.org/dom/range_intro.html

Tuesday, August 3, 2021
 
Envin
answered 4 Months ago
40

In IE, the line separator in a textarea's value property is rn. In all other major browsers it's n. A simple solution would be to normalize line separators into n first. I've also simplified the regex:

textareaIDelement.value.replace(/rn/g, "n").replace(/^(.*)n*$/gm, "[$1]");
Thursday, September 2, 2021
 
Jubair
answered 3 Months ago
67

There is no characters in range 128-255 for utf8, and ASCII ends completely at 127... Also the character at position 1 in "i[x98]g" is a "[", the "[x98]" is meaningless.

Your function can be replaced with str.charCodeAt(1).

The character ˜ is Unicode Character 'SMALL TILDE' (U+02DC and can be written as "u02DC", or String.fromCharCode(732)

Tuesday, October 19, 2021
 
job
answered 2 Months ago
job
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share