What is the REGEX of a CSS selector

前端 未结 9 2012
你的背包
你的背包 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 13:03

    It's true that you can not select css selectors using regex, but you can select css rules using regex, so you can make the job done in the inverse way:

    1. format css file in order to no have selectors and rules in the same line. your code should look like this:

      p{
      margin:0 0 10px;
      }
      .lead{
      margin-bottom:20px;
      font-size:21px;
      font-weight:200;
      line-height:30px;
      }

    2. select all rules (regex : (^(.)*;)

      p{
      margin:0 0 10px;
      }
      .lead{
      margin-bottom:20px;
      font-size:21px;
      font-weight:200;
      line-height:30px;

      }

    3. add some prefix to rules (example ####)

      p{
      ####margin:0 0 10px;
      }
      .lead{
      ####margin-bottom:20px;
      ####font-size:21px;
      ####font-weight:200;
      ####line-height:30px;

      }

    4. select all lines not beginning with ####,not a return to line, and not a } (regex : ^[^#### \n }])

      p{
      ####margin:0 0 10px;
      }
      .lead{
      ####margin-bottom:20px;
      ####font-size:21px;
      ####font-weight:200;
      ####line-height:30px;
      }

    5. add your css prefix

      .my_class_prefix p{
      ####margin:0 0 10px;
      }
      .my_class_prefix .lead{
      ####margin-bottom:20px;
      ####font-size:21px;
      ####font-weight:200;
      ####line-height:30px;
      }

    6. delete the added prefix ####

      .my_class_prefix p{
      margin:0 0 10px;
      }
      .my_class_prefix .lead{
      margin-bottom:20px;
      font-size:21px;
      font-weight:200;
      line-height:30px;
      }

提交回复
热议问题