jQuery/JavaScript Split String by Commas that are Outside of Parenthesis

后端 未结 1 578
臣服心动
臣服心动 2020-12-21 16:41

I am using jQuery & JavaScript!!

I have a string, for example

\"cubic-bezier(0.25, 0, 0.25, 1), ease-in, ease-out, linear\"

..

相关标签:
1条回答
  • 2020-12-21 17:03

    Match any comma not inside parentheses

    var myString = "cubic-bezier(0.25, 0, 0.25, 1), ease-in, ease-out, linear";
    
    var parts    = myString.split(/\,\s?(?![^\(]*\))/);
    
    console.log(parts)

    /\,\s?(?![^\(]*\))/
    
    • \, matches the character , literally
    • \s? matches any whitespace character. The ? quantifier matches between zero and one times
    • (?![^\(]*\)) Negative Lookahead asserts that the regex does not match a single character not present in the list below
    • [^\(] Quantifier. Matches between zero and unlimited times, as many times as possible
    • \( matches the character ( literally
    • \) matches the character ) literally
    0 讨论(0)
提交回复
热议问题