admin管理员组文章数量:1404612
I have some textual data in multiple lines, stored in an ES6 template string. Any line may contain a literal \n
string.
Example:
`line1
same\nline2
line3`
I want to split that string into an array of lines, where each line originates from a line of the template string, without splitting at a literal \n
within a line. So my expected / wanted result was a JavaScript array looking like this: ["line1", "same\nline2", "line3"]
.
When looking at the example below, this obviously doesn't happen when simply splitting using a regexp for line breaks (/\n/
).
So, is this possible at all? Am I missing / misunderstanding something on how template strings work?
const lines = `line1
same\nline2
line3`.split(/\n/);
document.getElementById('out').textContent = JSON.stringify(lines)
<pre id="out"></pre>
I have some textual data in multiple lines, stored in an ES6 template string. Any line may contain a literal \n
string.
Example:
`line1
same\nline2
line3`
I want to split that string into an array of lines, where each line originates from a line of the template string, without splitting at a literal \n
within a line. So my expected / wanted result was a JavaScript array looking like this: ["line1", "same\nline2", "line3"]
.
When looking at the example below, this obviously doesn't happen when simply splitting using a regexp for line breaks (/\n/
).
So, is this possible at all? Am I missing / misunderstanding something on how template strings work?
const lines = `line1
same\nline2
line3`.split(/\n/);
document.getElementById('out').textContent = JSON.stringify(lines)
<pre id="out"></pre>
Share
Improve this question
asked May 24, 2018 at 7:58
alex3683alex3683
1,56514 silver badges27 bronze badges
6
-
2
Problem is, a
\n
within a template literal is parsed identically to a literal linebreak - tryconsole.log
ging the string before splitting it. Maybe you wanted\\n
in the template literal instead? – CertainPerformance Commented May 24, 2018 at 8:00 -
Having
\\n
in the template literal would have been nice, but I exported the data from an SQL database. The output is in multiple lines with some\n
inbetween. I now escaped these line breaks in a text editor. My hope was to skip an intermediary step. – alex3683 Commented May 24, 2018 at 8:35 - @alex3683 Your SQL database did create JS code? I think you should add the appropriate escaping to that export function. – Bergi Commented May 24, 2018 at 10:00
- @Bergi No, not the JS code. Just the data in tab-separated values style. I just copy it out of the SQL manager and wanted to drop it into a template string in my JavaScript function. – alex3683 Commented May 24, 2018 at 10:58
- @alex3683 Yes, just this "copy out from the SQL result and place it in the JS code" process. If possible, you should automate that process and use a proper escaping mechanism, such as making the SQL engine output JSON (that's just the first search result I got). – Bergi Commented May 24, 2018 at 11:13
2 Answers
Reset to default 4You can use the String.raw
tag on your template literal:
const lines = (String.raw `line1
same\nline2
line3`).split(/\n/);
console.log(lines);
\n
is treated as a new line and regex will always match it. In your string you can escape \n
like:
`line1
same\\nline2
line3`
Splitting by \n
will give now:
["line1", "same\nline2", "line3"]
Beware that now \n
is just a string, not a new line - but you can map this array and replace it to the real new line.
["line1", "same\nline2", "line3"].map(value => value.replace(/\\n/g, "\n"))
本文标签: javascriptSplit template string on line breaks ignoring literal nStack Overflow
版权声明:本文标题:javascript - Split template string on line breaks ignoring literal n - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744835185a2627588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论