3

I've retrieved a CSS rule from document.styleSheets and am looking now to extract it's properties and values.

cssText = ".expl { position: absolute; background-color: rgb(204, 204, 204); max-width: 150px; }";

Is it possible with regular expressions to retrieve the properties and their appropriate values within a match? Plus, strip the trailing semi-colon.

I want to get the result as follows:

position: absolute // match 1
background-color: rgb(204, 204, 204) // match 2
max-width: 150px // match 3

I've only got to the point where I'm extracting what's within the brackets: (?<={)(.*)(?=}), have no clue with what should I continue.

How do I achieve this?

Thanks in advance!

3
  • Why not split the string in to array and loop through it ? Commented Oct 6, 2011 at 19:14
  • 1
    were you aware that cssText doesn't include the selector? so your example would be cssText = "position: absolute; background-color: rgb(204, 204, 204); max-width: 150px; " Commented Oct 6, 2011 at 19:16
  • You don't need a regexp for this at all. Use the CSS stylesheet DOM. quirksmode.org/dom/tests/stylesheets.html Commented Oct 6, 2011 at 19:20

4 Answers 4

4

You could just split the string on the ;

document.getElementById(id).style.cssText.split(";")

EDIT:

note that the cssText property of a style object does not contain the selector

enter image description here

EDIT 2:

Ok I did a little more digging and it appears you are getting your cssText property from a CSSStyleRule object. This includes the selectors. You can get a semicolon delimited list of the actual rules with a little more tree traversal. You can get the style object with

document.styleSheets[1].cssRules[0].style.cssText;

instead of

document.styleSheets[1].cssRules[0].cssText;

See this drill down:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Apparently someone is going through and just downvoting everything in this thread.
Oh, I guess should've descended one more time, heh.. Because you answered first, plus the effort- answer goes to you. Thanks!
Note: It is not 100% reliable. For instance, background-image:url("data:image/png;base64,.....");
3

Pull everything out between the brackets and then just split it out:

matches = cssrule.match(/{([^}]+)}/);
rules = matches[1].split(';');

5 Comments

oh! I see why you did that... I think the OP misunderstands though. the cssText property doesn't include the selector.
@Joseph, it retrieves selector on Chrome when gathered by cssRules. Maybe rules acts differently.
cssText is just happens to be his variable name. I think that's confusing the issue. It's not the cssText property.
@Cfreak no, he said he got it from document.styleSheets... I see what happened... going to update my answer again.
It actually is the cssText property, and as I've already said, it contains the selector. At least on Chrome it is.
2

My regexp is a bit rusty, but something like:

var arr = cssText.replace(/\s/g,'')
                 .replace(/^.*{([^}]+)}.*/,'$1')
                 .match(/([^;]+)/g);

should create:

["position:absolute","background-color:rgb(204,204,204)","max-width:150px"]

Comments

0

This breaks it all out into named items

\s*(?<rule>(?<selector>[^{}]+){(?<style>[^{};]+:[^{};]+;\s*)+})

If your parser doesn't support named groups (which javascript doesn't) remove the ?<text> from the regex, like this

\s*(([^{}]+){([^{};]+:[^{};]+;\s*)+})

Since your cssText may not contain the css selector, you may want to just split on ;. If it doesn't this works pretty good for parsing stylesheets. I used it in a tool I wrote to inline stylesheets.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.