Question

How to convert

"one,cubic-bezier(0, 0, 1, 1), cubic-bezier(0, 0, 1, 1), linear"

to

["one", "cubic-bezier(0, 0, 1, 1)", "cubic-bezier(0, 0, 1, 1)", "linear"]

How?

JavaScript.

This is not same. Becouse Quotation mark(left and right side), here ( - left, ) - right.

Don't work in JS.

'ease,cubic-bezier(0, 0, 1, 1), linear,2,3'.split(/,(?=[^()]*((|$))/gi); 

Result:

ease,(,cubic-bezier(0, 0, 1, 1),, linear,,2,,3
Was it helpful?

Solution

Assuming the parens cannot be nested (in which case you won't be able to use JS regex flavor).

Try this:

,(?![^()]*\))

A quick break down:

,          # match a comma                           [1]
(?!        # start negative look ahead               [2]
  [^()]*   #   match zero or more non-parens chars   [3]
  \)       #   match the closing paren               [4]
)          # stop look ahead

In plain English, that would read:

match a comma [1], only if there's no closing paren [4] ahead [2] without the presence of any paren between the comma and the closing paren [3]

OTHER TIPS

Based on your example:

var str = "one,cubic-bezier(0, 0, 1, 1), cubic-bezier(0, 0, 1, 1), linear";
var arr = str.split(/,\s*(?!\s*\d)/);
console.log(arr.toSource());

// result
// ["one", "cubic-bezier(0, 0, 1, 1)", "cubic-bezier(0, 0, 1, 1)", "linear"]

Here is the fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top