2012-07-23 44 views
2

我想在我的CSS的每一行的开始添加类CSS的每一行的正则表达式

我想

.more { 
    padding: 5px 10px 0; 
} 

#fbook { 
    display: inline; 
    float: left; 
    margin: 0 0 0 4px; 
    width: 320px; 
} 

看起来像加个班

.test .more { 
    padding: 5px 10px 0; 
} 

.test #fbook { 
    display: inline; 
    float: left; 
    margin: 0 0 0 4px; 
    width: 320px; 
} 

像这样的东西。

^([A-Za-z0-9]+)$ 

会工作,如果我的CSS看起来像这样

more 
{ 
    padding: 5px 10px 0; 
} 

fbook 
{ 
    display: inline; 
    float: left; 
    margin: 0 0 0 4px; 
    width: 320px; 
} 

,但我似乎无法得到任何工作时发现的。 #{

回答

2

试试这个

$result = preg_replace('/(?i)^([.#a-z]+\{)$/m', '.test $1', $subject); 

说明

" 
(?im)   # Match the remainder of the regex with the options: case insensitive (i);^and \$ match at line breaks (m) 
^    # Assert position at the beginning of a line (at beginning of the string or after a line break character) 
(   # Match the regular expression below and capture its match into backreference number 1 
    [.#a-z]  # Match a single character present in the list below 
        # One of the characters “.#” 
        # A character in the range between “a” and “z” 
     +    # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
    \{   # Match the character “{” literally 
) 
\$    # Assert position at the end of a line (at the end of the string or before a line break character) 
" 
+1

感谢您的帮助我无法完成这项工作,但您的详细解释让我足够前行! – ak85 2012-07-23 05:28:09

1

您可以使用现代化的工具,像LESS。只要把你的代码,并将其包装与.test{}

.test { 
    .more { 
     padding: 5px 10px 0; 
    } 

    #fbook { 
     display: inline; 
     float: left; 
     margin: 0 0 0 4px; 
     width: 320px; 
    } 
} 

如果你只需要一个时间,你可以在网上做:http://leafo.net/lessphp/#demo
如果你想时不时这样做,也有少可以使用多种语言的库,以及客户端JavaScript选项。

+0

我认为少,但没有多少访问我正在编辑的系统(只有css访问),所以查找和替换是我能想到的唯一方法,但是LESS在将来我会说会派上用场。 – ak85 2012-07-23 06:17:22

相关问题