-2

I have css that which follows..

<style>
.body{
    width:2px;
    height:3px;
    }
</style>
<style>
.anotherOne{
    key:value;
    ...........
    }
</style>

and the HTML part

<div class="body"></div>

So how can i get the value inside the 'body' class on first style tag ? also i want to append data there,how can i do with this javascript and regular expressions ?? is there any other way ?

I want to get anything inside that starting with ".body{" and ending with "}"

Edit :the expected result is width:2px; height:3px;

11
  • What is it that you're trying to accomplish? I don't think regexp are the suitable solution for this Commented Nov 10, 2011 at 5:11
  • I have a string that contain html data,i want to get the value inside curly bracket that start with body selector. Commented Nov 10, 2011 at 5:14
  • Where is this CSS? In your web page as parsed CSS styles? In a javascript dtring? Commented Nov 10, 2011 at 5:15
  • 2
    Is this for PHP or for Javascript? (Please don't tag-spam.) Commented Nov 10, 2011 at 5:17
  • 2
    Do you want to do this server-side (php) or client-side (javascript)? Commented Nov 10, 2011 at 5:19

2 Answers 2

1

If you have CSS styles isolated in a javascript string, you can get the width value with this regex:

var re = /\.body\s*\{[^\}]*?width\s*:\s*(.*);/m;
var matches = str.match(re);
if (matches) {
    var width = matches[1];
}

Test case here: http://jsfiddle.net/jfriend00/jSDeJ/.

The height value can be obtained with this regex:

\.body\s*\{[^\}]*?height\s*:\s*(.*);/m;

If you just want the text (whatever it is that's inside the curly brackets for the body tag, then you can do that with this:

var re = /\.body\s*\{([^\}]*?)\}/m;
var matches = str.match(re);
if (matches) {
    var width = matches[1];
}

You can see it work here: http://jsfiddle.net/jfriend00/jSDeJ/3/

If you then want to replace the entire .body style with your own string, you can do that like this:

var str = " <style>\n.body{\n width:2px; \n height:3px; \n  } \n</style>"; 

var re = /(\.body\s*\{)([^\}]*?)(\})/m;
var newStyle = "width: 20px; \nheight: 1000px;";
var result = str.replace(re, "$1" + newStyle + "$3");
alert(result);

You can see that work here: http://jsfiddle.net/jfriend00/jSDeJ/8/

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

13 Comments

I think you should constrain the regex to the style tag
I assumed he already had just styles in the string (much of this question is vague). If not, then the OP can do that first.
it returns 2px the expected one is width:2px; height:3px;
Replacement example added at the end of my answer.
Sure, you can remove the dot if you want. Just remove the slash before it too.
|
0

This looks promising...

https://github.com/sabberworm/PHP-CSS-Parser

Or, if you just want to know the "computed value", I'd rely on the browser's parser and just extract the values with javascript DOM methods.

Hope that helps...

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.