Created
March 7, 2018 09:24
-
-
Save kybernetikos/764e5063a492986f4539a2bcd929bf3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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