I'm trying to get a regular expression (in Javascript) to get margin, padding and border values, reading them from document.styleSheets
I wrote that:
/:\s*?(\d+)(px)?/gi
but is not good. The main problem is the the rule should be like:
margin: 3px;
margin: 3px 2px;
margin: 3px 2px 1px;
margin: 3px 2px 1px 0;
margin-top: 3px;
margin-right: 3px;
margin-left: 3px;
margin-bottom: 3px;
the same for the padding. For the border is similar:
border: 1px solid #000;
border-width: 1px 1px 2px 0;
border-top-width: 1px;
border-left-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-top: 1px solid #000;
border-left: 1px solid #000;
border-right: 1px solid #000;
border-bottom: 1px solid #000;
I can split the rule and search for each type, something like
if(rule.indexOf('margin') > -1) {
...
} else if (rule.indexOf('margin-top') > -1) {
...
}
and use different regex .. but I would like to have the more general regex to avoid a long list of "if".
Can anybody help me?
Many thanks, da