Skip to content

Instantly share code, notes, and snippets.

@BideoWego
Created November 25, 2017 21:39
Show Gist options
  • Save BideoWego/46873563aef0af055c7f5fb92546434e to your computer and use it in GitHub Desktop.
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
/*
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