Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove <script> tags from a string along with the content between them

I have looked and looked and looked but I couldn't find anything on this. So lets say I have this string...

var str = '<script>blah blah blah</script><a>...</a><div>...</div><p>...</p>';

I need to strip out the script tags out of the string along with all the content between the tags. Once the script tag is stripped I need to append it to a textarea. How in the world do I do this with jQuery?

Any help would be much appreciated!

like image 803
barrettathome Avatar asked Sep 06 '13 18:09

barrettathome


People also ask

How do I remove a specific tag from a string?

The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.

How can I remove only the script tag in PHP?

How to remove script tags from string in php? HTML; $dom = new DOMDocument(); $dom->loadHTML($html); $script = $dom->getElementsByTagName('script'); $remove = []; foreach($script as $item) { $remove[] = $item; } foreach ($remove as $item) { $item->parentNode->removeChild($item); } $html = $dom->saveHTML();

How do I remove a specific tag in HTML?

For HTML tags, you can press Alt+Enter and select Remove tag instead of removing an opening tag and then a closing tag.

How do I remove a script in HTML?

Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.


1 Answers

since nobody else is going to post a simple and reliably-working routine, i guess i will:

function noscript(strCode){
   var html = $(strCode.bold()); 
   html.find('script').remove();
 return html.html();
}


alert(   noscript("<script>blah blah blah</script><div>blah blah blah</div>")    );
//shows: "<div>blah blah blah</div>"
like image 159
dandavis Avatar answered Sep 19 '22 23:09

dandavis