Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #split #whitespace #quotes #javascript
ADD COMMENT
Topic
Name
8+7 =