Skip to content

Instantly share code, notes, and snippets.

@kybernetikos
Created March 7, 2018 09:24
Show Gist options
  • Save kybernetikos/764e5063a492986f4539a2bcd929bf3e to your computer and use it in GitHub Desktop.
Save kybernetikos/764e5063a492986f4539a2bcd929bf3e to your computer and use it in GitHub Desktop.
const line = '"" "a b" "a b c" "a \\" marvellous" \'we like\'a"b a""b abab abb "hi"';
const gre = /\s*(?:(?:"((?:\\"|[^"])*)")|(?:'((?:\\'|[^'])*)')|([^\s]+))/g;
function split(str) {
const result = [];
str.replace(gre, (part, dquoteMatch, squoteMatch, wsMatch, offset, fullString) => {
result.push(dquoteMatch !== undefined ? dquoteMatch : squoteMatch !== undefined ? squoteMatch : wsMatch)
});
return result;
}
console.log(split(line));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment