我已经从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 –