Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

javascript - JavaScript字符串换行符?(JavaScript string newline character?)

Is \n the universal newline character sequence in Javascript for all platforms?

(\n是否适用于所有平台的Java语言中的通用换行符序列?)

If not, how do I determine the character for the current environment?

(如果不是,我如何确定当前环境的角色?)

I'm not asking about the HTML newline element ( <BR/> ).

(我不是在问HTML换行符( <BR/> )。)

I'm asking about the newline character sequence used within JavaScript strings.

(我问的是JavaScript字符串中使用的换行符序列。)

  ask by Landon Kuhn translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(我刚刚使用了一些愚蠢的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 .

(Windows上的IE8和Opera 9使用\r\n 。)

All the other browsers I tested (Safari 4 and Firefox 3.5 on Windows, and Firefox 3.0 on Linux) use \n .

(我测试过的所有其他浏览器(Windows上的Safari 4和Firefox 3.5,Linux上的Firefox 3.0)都使用\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.

(设置值时,它们都可以很好地处理\n ,尽管IE和Opera会在内部再次将其转换回\r\n 。)

There's a SitePoint article with some more details called Line endings in Javascript .

(有SitePoint文章,其中有更多详细信息,称为Java中的行尾 。)

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).

(还要注意,这与HTML文件本身中的实际行尾无关( \n\r\n给出相同的结果)。)

When submitting a form, all browsers canonicalize newlines to %0D%0A in URL encoding.

(提交表单时,所有浏览器都将URL编码中的换行符规范化为%0D%0A 。)

To see that, load eg data:text/html,<form><textarea name="foo">foo%0abar</textarea><input type="submit"></form> and press the submit button.

(为此,请加载例如data:text/html,<form><textarea name="foo">foo%0abar</textarea><input type="submit"></form>并按提交按钮。)

(Some browsers block the load of the submitted page, but you can see the URL-encoded form values in the console.)

((某些浏览器阻止了已提交页面的加载,但是您可以在控制台中看到URL编码的表单值。))

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(/
|
|
/g);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...