0

Beginner at javascript/coding

I need to split a string every time the enter key is pressed and store them in an array

I-0000-DV-301-0003
I-0000-DV-301-0002
I-0000-DV-301-0001

output:
array[0] = I-0000-DV-301-0003
array[1]=I-0000-DV-301-0002
...

I've tried string.split but i'm not sure how to use it with the 'enter' or 'return' key.

Thank you!

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
aldenrj
  • 5
  • 2
  • how do you get the string to split ? – jonatjano Aug 06 '19 at 13:57
  • `Every time time the enter key is pressed` in: A user interacts with an input field and presses enter? Or do you have a complete string and try to split every carriage return? – empiric Aug 06 '19 at 13:57
  • This should just be marked as a duplicate, unless further informations are specified. As long as no informations about how the input is provided are available, this is a duplicate. – briosheje Aug 06 '19 at 13:59

2 Answers2

1

Split on a newline \n:

const str = `I-0000-DV-301-0003
I-0000-DV-301-0002
I-0000-DV-301-0001`;

const res = str.split("\n");

console.log(res);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
-1

To split with enter or new line character use this

str.split("\n");
Rahil Husain
  • 570
  • 3
  • 14