What is the REGEX of a CSS selector

前端 未结 9 2006
你的背包
你的背包 2021-02-02 12:27

I\'d like to parce a CSS file and add before every CSS selector another selector.

From:

p{margin:0 0 10px;}
.lead{margin-bottom:20px;font-size:21px;font-         


        
9条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 12:59

    So I've finally found a REGEX that works for my requirements

    ([^\r\n,{}]+)(,(?=[^}]*{)|\s*{)
    

    The key point was to add a Positive lookahead to avoid bad matches

    (?=[^}]*{)
    

    You can see the result here: http://regexr.com?328s7

    The only precaution that I can recommend is to remove every block comment:

    • bad sample with block comment: http://regexr.com?328sd

提交回复
热议问题