I have a string which looks like this: "foo, bar,baz"
I need your help with regexp to split it on commas that are surrounded by anything on both sides, e.g. ['foo, bar', 'baz']
I have a string which looks like this: "foo, bar,baz"
I need your help with regexp to split it on commas that are surrounded by anything on both sides, e.g. ['foo, bar', 'baz']
You may match all the substrings you need with /(?:\s,\S|\S,\s|[^,])+/g regex:
var regex = /(?:\s,\S|\S,\s|[^,])+/g;
var str = "foo, bar,baz";
console.log(str.match(regex));
See the regex demo
The regex matches 1+ occurrences of:
\s,\S - a whitespace, ,, non-whitespace| - or\S,\s - a non-whitespace, ,, whitespace| - or
-[^,] - a char other than ,