javascript split a string by whitespace code example

Example 1: split whitespace except in quotes javascript

// Hard to read, but works
// If you want to keep the \ in the string, replace the: c.replace(/\\(.)/,"$1"); by: c;

keywords.match(/\\?.|^$/g).reduce((p, c) => {
        if(c === '"'){
            p.quote ^= 1;
        }else if(!p.quote && c === ' '){
            p.a.push('');
        }else{
            p.a[p.a.length-1] += c.replace(/\\(.)/,"$1");
        }
        return  p;
    }, {a: ['']}).a

Example 2: how to saperate string to array

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