How to split on white spaces not between quotes?

\s(?=(?:[^'"`]*(['"`])[^'"`]*\1)*[^'"`]*$)

You can use this regex with lookahead to split upon.See demo.

https://regex101.com/r/5I209k/4

or if mixed tick types.

https://regex101.com/r/5I209k/7


The problem is that you need to exclude entries within the group. Instead of using a negative lookahead you could do it like this:

(\S*(?:(['"`]).*?\2)\S*)\s?|\s

Basically what it does is to:

  • captures any non-whitespace characters
    • that may contain a quoted string
    • and is optionally directly followed by any non-whitespace (e.g a comma after the quote).
  • then matches an optional trailing whitespace

OR

  • matches a single whitespace

Capture group1 will then contain an as long as possible sequences of all non-whitespace characters (unless they are within quotes). This can thus be used with the replacement group \1\n to replace your desired whitespaces with a newline.

Regex101: https://regex101.com/r/A4HswJ/1

JSFiddle: http://jsfiddle.net/u1kjudmg/1/