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 ?

Red
  • 6,230
  • 12
  • 65
  • 112
  • 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 https://developer.mozilla.org/en/DOM/DocumentFragment http://ejohn.org/blog/dom-documentfragments/ – Alex KeySmith Nov 25 '11 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. – Red Nov 25 '11 at 11:54
  • 1
    is there any difference put this "/n h1{a:b}/n" after . – erimerturk Nov 25 '11 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: http://gskinner.com/RegExr/ – Alex KeySmith Nov 25 '11 at 11:59
  • @erimerturk heheh..cool dude...you mad this Question absolutely waste. – Red Nov 25 '11 at 12:08
  • @Dileep Dil u are wellcome :) – erimerturk Nov 25 '11 at 12:10
  • Why is `mydata` even not HTML? – rds Nov 25 '11 at 12:35

5 Answers5

3

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

 str= str.replace("</style>",mydata+"</style>");
erimerturk
  • 4,230
  • 25
  • 25
  • 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. – T.J. Crowder Nov 25 '11 at 12:09
  • Thank you erimerturk,This is a good solution[My logic have some breaks]. – Red Nov 25 '11 at 12:12
  • @Crowder : I will never mind about Precedence [at least this time],anyway both are working well. – Red Nov 25 '11 at 12:15
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

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
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

Community
  • 1
  • 1
Arindam
  • 998
  • 1
  • 8
  • 20
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.

Community
  • 1
  • 1
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
0

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

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • 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] – Red Nov 25 '11 at 12:18