JSFiddle - React, Tailwind, and code Playground
JavaScript
s = 'research library "not available" author:"Bernard Shaw"'
function parse(str) {
var arr = [];
var quote = false; // true means we're inside a quoted field
// iterate over each character, keep track of column (of the returned array)
for (var i = c = 0; c < str.length; c++) {
var cc = str[c], nc = str[c+1]; // current character, next character
arr[i] = arr[i] || ''; // create a new column (start with empty string) if necessary
// If it's just one quotation mark, begin/end quoted field
if (cc == '"') { quote = !quote; continue; }
// If it's a space and we're not in a quoted field, move on to the next column
if ((cc == ' ') && !quote) { ++i; continue; }
// Otherwise, append the current character to the current column
arr[i] += cc;
}
return arr;
}
console.log(parse(s));