I have a textbox in reactjs. I want to slice its contents by new lines, outputting an array.
For example, if the textbox has
foo
bar
foobar
I want to get
['foo', 'bar', 'foobar']
Any ways I can do this?
CodePudding user response:
\n means new line, I suggest trimming the text before splitting.
string.trim().split('\n')
CodePudding user response:
for that, you just need to use 'split' function with '\n' which will help you separate the text.
codesandbox example : https://codesandbox.io/s/vibrant-murdock-0rmku2?file=/src/App.js
CodePudding user response:
We can use split() method here
let's consider an example
str = "x\ny";
console.log(str.split("\r\n"));
you may need to use '\r\n'
the output is
["x", "y"]