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

I looked, looked and looked, but I could not find anything about this. So let's say that I have this line ...

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

I need to cut script tags from a string along with all the content between the tags. Once the script tag is removed, I need to add it to the text box. How in the world do I do this with jQuery?

Any help would be greatly appreciated!

+6
source share
4 answers

since no one is going to publish a simple and reliable working procedure, I think that:

 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>" 
+10
source

Try the following: -

 var str= "<script>blah blah blah</script><div>blah blah blah</div>"; var html = $(str.bold()); html.find('script').remove(); 
+1
source

Same:

 var div = $('<div>').html(str); div.find('script').remove(); str = div.html(); 

Example: http://codepen.io/paulroub/pen/IabsE

0
source

Here is the fiddle ( https://jsfiddle.net/skoley/9dnmbj3d/ ) of the answers that are best used in several other sources, credit suits them.

 function noscript(strCode){ var html = $(strCode.italics()); html.find('script').remove(); return html.html(); } function noscript_alt(str) { var div = $('<div>').html(str); div.find('script').remove(); str = div.html(); return str;} 
0
source

Source: https://habr.com/ru/post/953351/


All Articles