1

Good evening, I am attempting to extract multiple variables in a line which is separated by tabs. Here is an example of the lines:

ALPRAZOLAM XANAX ANXIETY F41.1
ALPRAZOLAM XANAX DEPRESSION F33.0

How would one go about extracting the individual values from this line? Thank you!

  • 1
    Split by line break, iterate over the list of strings and split each one by tab (or whitespace). https://stackoverflow.com/q/96428/218196 – Felix Kling Jan 25 '19 at 18:41
  • 1
    Possible duplicate of [JavaScript split String with white space](https://stackoverflow.com/questions/26425637/javascript-split-string-with-white-space) – Calvin Nunes Jan 25 '19 at 18:42

3 Answers3

4

const x = `ALPRAZOLAM XANAX ANXIETY F41.1
ALPRAZOLAM XANAX DEPRESSION F33.0`;

const y = x.split("\n").flatMap(el => el.split(" "));

console.log(y);
Aziz.G
  • 3,599
  • 2
  • 17
  • 35
1

You can split it on the basis of space and adding them into an array like this

var string = "ALPRAZOLAM XANAX DEPRESSION F33.0";
var array = string.split( "\n" );
console.log(array); 

For splitting it on the basis of new line and whitespace use this

var string = "ALPRAZOLAM XANAX DEPRESSION F33.0";
var array = string.split(/\s+/);
console.log(array);
Ritviz Sharma
  • 306
  • 1
  • 9
0

Done it with plain JS

var input = "ALPRAZOLAM XANAX ANXIETY F41.1";
var arr = [];

arr = input.split(' ');

for (var i = 0; i < arr.length; i++){
  console.log(arr[i]);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Rod Ramírez
  • 1,138
  • 11
  • 22