我已經從document.styleSheets
中檢索了一條CSS規則,現在正在尋找提取它的屬性和值。正則表達式匹配CSS「<property>:<value>」
cssText = ".expl { position: absolute; background-color: rgb(204, 204, 204); max-width: 150px; }";
是否有可能使用正則表達式檢索匹配內的屬性及其相應的值?另外,去掉尾隨的分號。
我想要得到的結果如下:
position: absolute // match 1
background-color: rgb(204, 204, 204) // match 2
max-width: 150px // match 3
我只有到我提取括號內有什麼意義:(?<={)(.*)(?=})
,有什麼要我繼續毫無頭緒。
我該如何做到這一點?
在此先感謝!
爲什麼不是字符串通過它拆分數組和循環? –
你是否知道cssText不包含選擇器?所以你的例子是'cssText =「position:absolute; background-color:rgb(204,204,204); max-width:150px;」' –
你根本不需要regexp。使用CSS樣式表DOM。 http://www.quirksmode.org/dom/tests/stylesheets.html –