0

see - > i have the following text in a variable

var s = 

    "<html>
  <head>
    <style type="text/css" >     
      body{
 background:url(/a.png);
          }
    </style>
    <script src="2.js"></script>
    <script src="1.js"></script>
  </head>
  <body >
   {BODYCONTENT}
  </body>


</html>";

Now i want to append some data immediately after closing of <style> tag's > .

Ex :

var mydata = "/n h1{a:b}/n";

So i want

<style>/n h1{a:b}/n

it is very easy if there is no other attribute in style tag,but sometime there may be some other attributes like type ,id

ex :

<style type="text/css" id="default_css_scope">
content
</style>

so that i cant use like this s.replace("<style>","<style>".mydata);

How can i do this with Regular Expression ?

7
  • 1
    This doesn't answer your question directly, but you may wish to look at "DocumentFragments" as an alternative to regex. This allows you to use dom functions directly on dettached dom elements developer.mozilla.org/en/DOM/DocumentFragment ejohn.org/blog/dom-documentfragments Commented Nov 25, 2011 at 11:52
  • @AlexKey Actually i am not going to render this at this time,i am making a WYSWYG editor,so its not a good idea to use DOm methods better than string manipulation. Commented Nov 25, 2011 at 11:54
  • 1
    is there any difference put this "/n h1{a:b}/n" after <style> and before </style>. Commented Nov 25, 2011 at 11:57
  • 1
    Cool, that makes sense. Must admit I'm not a Regex guru, but this site provides a nice online editor that's helped me in the past: gskinner.com/RegExr Commented Nov 25, 2011 at 11:59
  • @erimerturk heheh..cool dude...you mad this Question absolutely waste. Commented Nov 25, 2011 at 12:08

5 Answers 5

3

maybe you should try this. i think it will solve your problem.

 str= str.replace("</style>",mydata+"</style>");
Sign up to request clarification or add additional context in comments.

3 Comments

Nice end-run, good to think outside the box like that. It could matter that he add the new stuff at the top rather than bottom of the style block (because of precedence), but if not, it simplifies things for him.
Thank you erimerturk,This is a good solution[My logic have some breaks].
@Crowder : I will never mind about Precedence [at least this time],anyway both are working well.
1

Caveat: I do not recommend using regular expressions to work extensively with HTML.

But you might get away with it in this specific case:

str = str.replace(/<style[^>]*>/, function(m) {
    return m + mydata;
});

Live example

3 Comments

Hai,Is there any other way ? I am making a WYSWYG editor.So i dont want to convert this to DOM,just want to save on DB.
@DileepDil: My answer doesn't convert anything to DOM. It does a string replacement on text in a variable.
@Crowder i mean the First line of your Answer.
1

Kindly refer this. A Brilliant post from sometime ago on the use of Regex with HTML.

Two of the most diverse technologies used together.

[See here] RegEx match open tags except XHTML self-contained tags

Comments

0

Something like this

var start = html.indexOf("<script", 0); //find the index of the first script
start = html.indexOf(">", start); //Starting for that script tag, find the next >

var output = [html.slice(0, start), myData, html.slice(start)].join(''); //Add string to at that position.

Thanks to jAndy for his code snippit

Hope this helps.

Comments

0

You might want to look into the HTML Agility Pack (free library):

1 Comment

A simple solution is always welcomed.Also No more DOM [This is different case,if i am doing this on a web page then its welcomed]

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.