I want to remove <script>
tags from an html string using regex.
I have the following code which works, but doesn't work when you put back to back scripts:
function removeScriptsFromHtmlStr(html) {
const regex = /<script(?:(?!\/\/)(?!\/\*)[^'"]|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\/\/.(?:\n)|\/\*(?:(?:.|\s))*?\*\/)*?<\/script>/;
const result = html.replace(regex, '');
return result;
}
So for example:
running this through the funciton will work fine
<script>alert(document.cookie);</script>
but this won't:
<script>alert(document.cookie);</script><script>alert(document.cookie);</script>
How can I update the regex to fix this?