dongyong5255 2011-08-18 02:56
浏览 145
已采纳

CSS选择器的正则表达式 - PHP

Does anyone know what the most robust regex would be to select all selectors from a css declaration? Below is CSS code for non-standard CSS that would be a good test case.

.somenormalstyledeclaration 
{
    width:100%;
    height:100%;
    background-image: url(images/fallback-gradient.png);
}
.somenormalstyle1, .somenormalstyle2 
{
    width:100%;
    height:100%;
    background-image: url(images/fallback-gradient.png);
}
.gradient-bg 
{
   background-color: #1a82f7;
   background-image: url(images/fallback-gradient.png);
   background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#2F2727), to(#1a82f7));
   background-image: -webkit-linear-gradient(top, #2F2727, #1a82f7);
   background-image:    -moz-linear-gradient(top, #2F2727, #1a82f7);
   background-image:     -ms-linear-gradient(top, #2F2727, #1a82f7);
   background-image:      -o-linear-gradient(top, #2F2727, #1a82f7);
}

@-webkit-keyframes bounce_circle 
{
    0% 
    {
        opacity:0.3;
    }
    50% 
    {
        opacity:1;
        background-color:#111
    }
    100% 
    {
        opacity:0.3;
    }
}
  • 写回答

2条回答 默认 最新

  • douhuang2673 2011-08-18 04:01
    关注

    This should work mostly. It's a few steps, but not as good as a full parser.

    1. Replace all newlines with a space character
    2. Assuming you never use strings including { or }, you can replace /\{[^\}]\}/ with a ,
    3. Split all the text into an array on each , and ; character
    4. Trim all whitespace from the beginning and end of each selector

    Every element of the array should be a selector or an "at-rule" (i.e. @charset utf-8;).

    I have probably missed some other edge cases, but this should work as a quick-and-dirty solution for most cases.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?