简体   繁体   English

javascript中字符串中的转义序列

[英]escape sequences in strings in javascript

i want to write this sentence in 3 lines but on scrimba this does not work but on Repl.lt ( https://repl.it/repls/WickedBaggyRegression ) it works, why? 我想在3行中写这句话,但是在scrimba上却不起作用,但是在Repl.lt( https://repl.it/repls/WickedBaggyRegression )上却可以,为什么?

var myStr = 'FirstLine\n\\SecondLine\\\rThirdLine';
console.log(myStr);

It is probably due to how scrimba.com site does interpret and displays your log in its own custom console. 这可能是由于scrimba.com网站如何在其自定义控制台中解释和显示您的日志。
(it probably wrap the text console.log receives in a single line.) (它可能会将console.log接收的文本包装在一行中。)

Try by replacing console.log by alert and you will see escape sequences are "preserved" in the alert popup displayed in the simulated browser, which means, they are correct in a JavaScript way : 尝试用alert替换console.log ,您将在模拟浏览器中显示的警报弹出窗口中看到转义序列被“保留”,这意味着它们以JavaScript方式是正确的:

I also found a course that explain this: https://scrimba.com/p/p4Mrt9/c4vJdha 我还找到了一门解释这一问题的课程: https : //scrimba.com/p/p4Mrt9/c4vJdha

在此处输入图片说明

If you open up the browser console, you can see that the escape characters work perfectly fine. 如果打开浏览器控制台,则可以看到转义符工作正常。 Please don't rely on the inbuilt console of tutorial sites, for the credibility of Javascript. 为了保证Javascript的信誉,请不要依赖教程站点的内置控制台。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM