[英]How to split multiline string into lines?
How to split a multiline string into lines without taking into account the newline in the line itself?如何在不考虑行本身的换行符的情况下将多行字符串拆分为行?
My approach is not working:我的方法不起作用:
const str = `
Hello
World\t
its\nbeautiful
`;
JSON.stringify(str).split(/$\\n/g)
What is the result of this approach:这种方法的结果是什么:
[""", "Hello", "World\t", "its", "beautiful", """]
What result is needed in the end:到底需要什么结果:
[""", "Hello", "World\t", "its\nbeautiful"]
Since \n
is the character that marks new lines, just like a normal new line, there is no way for javascript to differenciate between \n
and \n
.由于
\n
是标记新行的字符,就像普通的新行一样,javascript 无法区分\n
和\n
。
An idea for your "special" new lines could be to escape that \n
with another \ so you would end up with你的“特殊”新行的一个想法可能是用另一个 \ 来逃避
\n
,这样你最终会得到
const str = `
Hello
World\t
its\\nbeautiful
`;
str.split("\n");
The result would be this:结果将是这样的:
['', 'Hello', 'World\t', 'its\\nbeautiful', '']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.