2014-10-04 172 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

相關問題