In JavaScript, you can split a string by new line using the split
method with the regex /\n/
.
In this example,
str
with a value containing new lines.split
method with the regex /\n/
to split the string into an array of substrings.const str = 'Hello\nWorld\n';
const substrings = str.split(/\n/);
substrings.forEach(sub => console.log(sub));
Hello World
In this tutorial, we learned How to Split a String by New Line in JavaScript language with well detailed examples.