Ole
Ole

Reputation: 46940

Removing first element of array if it contains only one or more spaces

I have a file with values like these:

 235 231 53t242354
  45 234   2354235
   3  53   

I turn each line into an array using this expression:

 //Split on one or more spaces
 let arr = line.split(/\s+/);

In cases where the first column starts with spaces, the first column in the array will contain the spaces. How do I detect and remove the first column in this case?

Upvotes: 1

Views: 146

Answers (2)

ibrahim mahrir
ibrahim mahrir

Reputation: 31692

Try using match with this regex: /\S+/g (S in uppercase) that matches non-space sequence of characters:

let line = "  56 77    90";

let arr = line.match(/\S+/g);

console.log(arr);

Upvotes: 1

charlietfl
charlietfl

Reputation: 171669

Use trim() to remove whitespace

let arr = line.trim().split(/\s+/);

Upvotes: 4

Related Questions