javascript - Parse HTML string containing script tag and document.write -
i have below string. has nested document.write string statements. want add text contents of innermost script document.
"document.write('<script>document.write(\"<script>document.write(\"hello world\");<\/script>\");<\/script>')"
how can parse string hello world gets added in document. e.g. html output can below.(can in body or div, ok.)
<body>hello world</body>
p.s. there can number of nested document.write statements. need parse string can handle n level of nesting.
well figured out now.
var str = "document.write('<script>document.write(\"<script>document.write(\"hello world\");<\/script>\");<\/script>')"; var astr, scriptele = document.createelement('script'); astr = str.replace(/["']/g, '"'); astr = astr.replace(/"<script>document.write/g, ""); astr = astr.replace(/;<\/script\>"/g, ""); scriptele.innerhtml = astr; // console.log(astr); document.body.appendchild(scriptele);
this handles n level of nesting.
Comments
Post a Comment