2012-07-23 43 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

相關問題