2014-10-04 99 views
2

好的,我有这种情况,逗号在括号内,我想匹配只在括号外的逗号。正则表达式仅匹配逗号,但不包含在多个括号内

Input : color-stop(50%,rgb(0,0,0)), color-stop(51%,rgb(0,0,0)), color-stop(100%,rgb(0,0,0))) 

Output(i'm looking for):color-stop(50%,rgb(0,0,0))**,** color-stop(51%,rgb(0,0,0))**,** color-stop(100%,rgb(0,0,0))) 

这是我的正则表达式: -

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

不幸的是,它并没有当有多个括号:(

Regex

+2

使用CSS解析器,而不是! – Ryan 2014-10-04 18:43:44

回答

5

这里的工作这个正则表达式对您的输入非常有用

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

DEMO

说明:

,      ',' 
(?!      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

它的工作!谢谢你,你救了我的生命:) – 2014-10-04 18:51:45

+0

很高兴拯救你的生命:-)。因为你是堆栈溢出的新手,所以我建议你阅读这个http://stackoverflow.com/help/accepted-answer – 2014-10-04 18:55:56

+0

你介意解释它是如何工作的,这看起来像是乱码:)? – 2014-10-04 19:40:44

相关问题