You do not need to use a regular expression, because those can be easy to trick and are not fit for parsing HTML content, especially not untrusted HTML content.
Instead, you can use a DOMParser to create a new document and use the DOM API to find and remove all script tags, then return the rest of the content:
function sanitise(input) {
const parser = new DOMParser();
const doc = parser.parseFromString(input, "text/html");
//find all script tags
const scripts = doc.getElementsByTagName('script');
for (const script of scripts)
script.remove(); //remove from the DOM
return doc.body.textContent.trim();
}
//using the + because otherwise Stack Snippets breaks
console.log(sanitise("hello <script>alert('I am stealing your data');</scr"+"ipt>"))
scripttag, I wouldn't assume it's constant. This appears to be aimed at sanitising arbitrary data.