Split on newline only

I have this textarea, and the value is -
Hello
World\n
Test

I want it to split on the newline only, not the \n. So the desired output would be -
[“Hello”, “World\n”, “Test”]

You can just simply split the string with \n, since the \n part in the string is actually \\n:

array.split('\n');

If you want to be sure you are splitting it by the newline, you can just enter one using backticks:

array.split(`
`);
4 Likes

Is this for python or JavaScript? The cat says it is python but your answer is for js.

Ik about the cat edit by @SharkCoding

2 Likes

Good point. @OmegaOrbitals’s post and @savardo’s reply indicate JS, but the category indicates Python.
Just in case, to do it in Python:

your_string = '''A
really
annoying
string'''
your_string = 'A\nreally\nannoying\nstring' # Either of these variables has
# multiple lines and so either of them work.
your_string = your_string.split('\n')
3 Likes

yes, that was a mistake :face_with_spiral_eyes:
I guess I was for some reason thinking it had the wrong tag bc it didn’t just say JS, I’m guessing this is meant to be in JS though

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.