0

I have a JavaScript variable:

var s =   
 "<html><head><style>body{a:b;c:d}</style></head><body></body></html>";

First, I am extracting the content inside <style> tags:

s = s.split(/(<style[^>]*>|<\/style>)/i)[2]; 

// s == "body{a:b;c:d}";

Then I am doing something with s and after the manipulation I need to append it on <style> tags.

How can I insert s between <style> and </style> ?

Example: Modified var s = "body{x:h;f:l;}";

so I need the resulting content like this:

 "<html><head><style>body{x:h;f:l;}</style></head><body></body></html>";

Update: jQuery is allowed - But only string manipulation is allowed. Pure JavaScript solutions have first priority.

3
  • If you are allowed to use jQuery the answer to this would be much simpler. Please indicate that. Commented Nov 25, 2011 at 6:03
  • jQuery is allowed ,But a neat Javascript solution goes priority.Also i cant convert this to DOM. Commented Nov 25, 2011 at 6:04
  • cool, then yeah the regex solution below seems like the best way. Commented Nov 26, 2011 at 18:04

3 Answers 3

3
var s  = "<html><head><style>body{a:b;c:d}</style></head><body></body></html>";
var b = s.split(/(<style[^>]*>|<\/style>)/i)[2]; 
s = s.replace(b,"body{x:h;f:l;}");

finally, s is what you want.

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

Comments

0

Does this work for you:

var s  ="body{a:b;c:d}";
    var newS = s.replace("body{a:b;c:d}", "body{x:h;f:l;}");
    alert(s);
    alert(newS);

1 Comment

Please take a look at the Question : this is the one that i want to append,please tell me how can i append the news to style tags.
0

Use below mentioned code

var someHTML = "<html><head><style>body{a:b;c:d}</style></head><body></body></html>";
var styleContents = someHTML.split(/(<style[^>]*>|<\/style>)/i)[2];
document.getElementById('style').innerHTML = styleContents;

1 Comment

Sorry,i cant convert to DOM.Last line makes problems,i cant access like document.getElementById('style').innerHTML ,because all thease are strings.

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.