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 ?
maybe you should try this. i think it will solve your problem.
str= str.replace("</style>",mydata+"</style>");
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;
});
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
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.
您可能需要研究HTML Agility Pack (免费库):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.