[英]String within a string can't be split by newline char
I don't really know why this is the case - 我真的不知道为什么会这样-
a string literal like the following is ""double-encoded""
: 像下面这样的字符串文字是
""double-encoded""
:
" => Suman => fatal error in suite with path=\"/Users/amills001c/WebstormProjects/oresoftware/suman/test/build-tests/test6.js\"\n (note: You will need to transpile your test files if you wish to use ES7 features)" => error => "SyntaxError: Unexpected reserved word\n at exports.runInThisContext (vm.js:53:16)\n at Module._compile (module.js:373:25)\n at Object.Module._extensions..js (module.js:416:10)\n at Module.load (module.js:343:32)\n at Function.Module._load (module.js:300:12)\n at Module.require (module.js:353:17)\n at require (internal/module.js:12:17)\n at Domain.<anonymous> (/Users/amills001c/WebstormProjects/oresoftware/suman/lib/run-child.js:33:5)\n at Domain.run (domain.js:228:14)\n at Object.<anonymous> (/Users/amills001c/WebstormProjects/oresoftware/suman/lib/run-child.js:32:3)"
the reason it's double encoded is because it was created like: 之所以采用双重编码,是因为它的创建方式如下:
var str = "foo" + JSON.stringify(bar) + "baz";
is there a good reason why I won't be able to then split the resulting string with String(str).split('\\n')
? 有充分的理由为什么我无法使用
String(str).split('\\n')
分割结果字符串吗? Seems to be the case, just curious as to why that is. 似乎是这样,只是好奇为什么会这样。
The problem is not in double-quoting. 问题不在双引号中。 The issue is that
JSON.stringify
escapes all the special characters in the string. 问题是
JSON.stringify
转义字符串中的所有特殊字符。 So, basically your \\n
in the resulting string is not a new-string character, but two characters "\\" and "n". 因此,结果字符串中的
\\n
基本上不是新字符串字符,而是两个字符“ \\”和“ n”。 To achieve what you want use str.split('\\\\n')
要实现您想要的,请使用
str.split('\\\\n')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.