Regex to match only comma's but not inside multiple parentheses

前端 未结 1 400
长情又很酷
长情又很酷 2020-12-06 21:33

Okay, i have this case where comma are inside parenthesis, I want to match the commas that are only outside the parenthesis.

Input : color-stop(50%,rgb(0,0,0         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 21:58

    Here is the regex which works perfectly for your input.

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

    DEMO

    Explanation:

    ,                        ','
    (?!                      look ahead to see if there is not:
      [^()]*                   any character except: '(', ')' (0 or
                               more times)
      (?:                      group, but do not capture (optional):
        \(                       '('
        [^()]*                   any character except: '(', ')' (0 or
                                 more times)
        \)                       ')'
      )?                       end of grouping, ? after the non-capturing group makes the
                               whole non-capturing group as optional.
      \)                       ')'
    )                        end of look-ahead
    

    0 讨论(0)
提交回复
热议问题