/* GroupBegin General */
.style1 {
padding-top:50px;
padding-left:50px;
}
/* GroupEnd */
/* GroupBegin test11 */
#test {
margin-top:50px;
margin-left:50px;
margin-bottom:50px;
}
test1{
padding-top:50px;
}
test2,test3{
word-spacing:normal;
padding-top:50px;
}
/* GroupEnd */ 想把这串css解析成一个json 字符串 格式如下
["General":{".style1":{"padding-top:50px;","padding-left:50px;"},},"#test ":{"margin-top:50px;","margin-left:50px; ","margin-bottom:50px; "},},"test1":{"padding-top:50px; "},},"test2":{"word-spacing:normal; ","padding-top:50px; "},},"test3":{"word-spacing:normal; ","padding-top:50px; "},}]
谢谢
.style1 {
padding-top:50px;
padding-left:50px;
}
/* GroupEnd */
/* GroupBegin test11 */
#test {
margin-top:50px;
margin-left:50px;
margin-bottom:50px;
}
test1{
padding-top:50px;
}
test2,test3{
word-spacing:normal;
padding-top:50px;
}
/* GroupEnd */ 想把这串css解析成一个json 字符串 格式如下
["General":{".style1":{"padding-top:50px;","padding-left:50px;"},},"#test ":{"margin-top:50px;","margin-left:50px; ","margin-bottom:50px; "},},"test1":{"padding-top:50px; "},},"test2":{"word-spacing:normal; ","padding-top:50px; "},},"test3":{"word-spacing:normal; ","padding-top:50px; "},}]
谢谢
格式都错了吧
function change(css){
var groups = [],
p0 = /\/\*(.+)\*\//g,
p1 = /\/\* GroupBegin (.+) \*\//g,
p2 = /\/\*\s+GroupEnd\s+\*\//g,
p3 = /\/\* GroupBegin (.+) \*\/(.*)\/\*\s+GroupEnd\s+\*\//g,
p4 = /\s*(\S+)\s*{([^}]*)}/g,
p5 = /([^:]+)\s*:\s*([^;]+);/g;
css.replace(/\s*(\r\n|\n)\s*/g, '').replace(p0, function(m, $1){
return p1.test(m) || p2.test(m) ? m : '';
}).replace('*\/\/*', '*\/\n\/*').replace(p3, function(m, $1, $2){
var styles = [];
$2.replace(p4, function(m, $1, $2){
var sets = [];
$2.replace(p5, function(m, $1, $2){
sets.push('"' + $1 + ':' + $2 + ';"');
});
styles.push('"' + $1 + '":{' + sets.join(',') + '}');
});
groups.push('"' + $1 + '":{' + styles.join(',') + '}');
});
return '{' + groups.join(',') + '}';
}
</script>
<textarea onchange="alert(change(this.value))" style="width:200px; height:200px;">
/* GroupBegin General */
style1 {
background-image:None;
word-spacing:normal;
padding-top:50px;
padding-left:50px;
}
/* GroupEnd */
/* GroupBegin test11 */
test {
margin-top:50px;
margin-left:50px;
margin-right:50px;
margin-bottom:50px;
}
test1{
word-spacing:normal;
padding-top:50px;
}
/* GroupEnd */
</textarea>