split string by space js code example

Example 1: js split text on spaces

var string = "text to split";
var words = string.split(" ");

Example 2: javascript explode

//split into array of strings.
var str = "Well, how, are , we , doing, today";
var res = str.split(",");

Example 3: split by whitespace javascript

var text = "hoi how     are          you";
var arr = text.split(/\s+/);

Example 4: javascript split

var names = 'Harry ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ';

console.log(names);

var re = /\s*(?:;|$)\s*/;
var nameList = names.split(re);

console.log(nameList);

Example 5: how to saperate string to array

Scanner in=new Scanner(System.in);
		String input=in.nextLine();
		String[] word=input.split(" ");