Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split string with newline ('\n') in Node?

Within Node, how do I split a string using newline ('\n') ? I have a simple string like var a = "test.js\nagain.js" and I need to get ["test.js", "again.js"]. I tried

a.split("\n");
a.split("\\n");
a.split("\r\n");
a.split("\r");

but the above doesn't work.

like image 789
PaolaJ. Avatar asked Oct 16 '22 05:10

PaolaJ.


People also ask

How do I split a string into a newline?

Split String at Newline Split a string at a newline character. When the literal \n represents a newline character, convert it to an actual newline using the compose function. Then use splitlines to split the string at the newline character. Create a string in which two lines of text are separated by \n .

How do you split a string in node?

The split() function is a string function of Node. js which is used to split string into sub-strings. This function returns the output in array form. Parameters: This function accepts single parameter separator which holds the character to split the string.

How can I put strings in an array split by New line?

To split a string by newline, call the split() method passing it the following regular expression as parameter - /\r?\ n/ . The split method will split the string on each occurrence of a newline character and return an array containing the substrings.

How do you split a string into a new line in Python?

To split a string by newline character in Python, pass the newline character "\n" as a delimiter to the split() function. It returns a list of strings resulting from splitting the original string on the occurrences of a newline, "\n" .

How to split string with Newline(’) in Node JS?

To split string with newline (‘ ’) in Node.js, we can use the string split method. to call "a b c".split with the regex /? / to split the string by or into separate strings. To split string with newline (‘ ’) in Node.js, we can use the string split method.

How to split a string by new line character in JavaScript?

We will call the split () method on this string with new line character passed as argument. split () method splits the string by new line character and returns a list of strings. The string can also contain characters in the string as shown below, instead of a multi-line string with triple quotes.

How do you split a string by new line in Python?

Example 1: Split String by New Line using str.split () In this example, we will take a multiline string string1. We will call the split () method on this string with new line character n passed as argument. split () method splits the string by new line character and returns a list of strings.

How do you split a string by line in Unix?

Split String by Newline Using Regular Expressions Next, let's start by looking at the different characters used to separate lines in different operating systems. The “ ” character separates lines in Unix, Linux, and macOS. On the other hand, the “ ” character separates lines in Windows Environment.


1 Answers

Try splitting on a regex like /\r?\n/ to be usable by both Windows and UNIX systems.

> "a\nb\r\nc".split(/\r?\n/)
[ 'a', 'b', 'c' ]
like image 103
maerics Avatar answered Nov 10 '22 22:11

maerics