Created
November 25, 2017 21:39
-
-
Save BideoWego/46873563aef0af055c7f5fb92546434e to your computer and use it in GitHub Desktop.
Regex to capture command-line arguments separated by spaces but group strings surrounded in quotes
This file contains hidden or 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
/* | |
Modified from this SO post: | |
https://stackoverflow.com/questions/24069344/split-spaces-avoiding-double-quoted-js-strings-from-a-b-c-d-to-a | |
*/ | |
/('|")(?:\\('|")|\\\\|[^'|"])*('|")|\S+/ | |
"a b 'c d'".match(/('|")(?:\\('|")|\\\\|[^'|"])*('|")|\S+/g) | |
//=> [ 'a', 'b', '\'c d\'' ] | |
'a b "c d"'.match(/('|")(?:\\('|")|\\\\|[^'|"])*('|")|\S+/g) | |
//=> [ 'a', 'b', '"c d"' ] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment