PROWAREtech
JavaScript: Remove SCRIPT Elements from HTML Using Regular Expressions
Use regular expressions to remove SCRIPT elements from HTML text.
This simple code will replace script tags and everthing contained between them with an empty string. The exception is when there is a string contained within the script tag that has the text </script>
somewhere in it. This will break it, but some careful programming can overcome this.
Replace links in text with HTML Anchors.
var html = `<p>This is a test</p>
<script type='text/javascript'>
document.write(123);
</script>
<p>The script tag should have been removed!</p>`;
var scriptRegex = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
var html2 = html.replace(scriptRegex, "");
console.log(html2);
alert(html2);
Comment