digit
digit

Reputation: 4565

Splitting string with random white spaces

I want to split a string like

"John   [email protected]     +3091424213                Hawai" 

with text.split(" ") but it splitting the first space only. I've tried put + text.split(" +"), but i get

John  [email protected]       +3091424213             Hawai   
undefined 
undefined 
undefined 

same with text.split("\s+"), the output is the same.

My code

  var splitText = text.split(" +");
  console.log(splitText[0]);
  console.log(splitText[1]);
  console.log(splitText[2]);
  console.log(splitText[3]);

Thanks for the help!

Upvotes: 0

Views: 246

Answers (1)

taile
taile

Reputation: 2776

You can use a regular expression:

var string = "John   [email protected]     +3091424213                Hawai";
string.split(/\s* \s*/);

Upvotes: 2

Related Questions