Reputation: 5183
I have some string with html tags.
var str = '<a href="www.com">text</a>
<script>
//some code etc
</script>
............... etc
';
I need to remove <script>....</script> using regexp with js's replace() function. Could not figure how to do it.
My efforts were:
/(<script).(</script>)/m
/<script.*>([\s\S]*)</script>/m
/(<script)*(</script>)/
/<script*</script>/
no success =(
Upvotes: 1
Views: 164
Reputation: 490143
Try...
/<script>[\s\S]*<\/script>/
If this is for arbitrary HTML, consider using DOM manipulation methods instead.
var fauxDocumentFragment = document.createElement("div");
fauxDocumentFragment.innerHTML = str;
var scriptElements = fauxDocumentFragment.getElementsByTagName("script");
while (scriptElements.length) {
scriptElements[0].parentNode.removeChild(scriptElements[0]);
}
If you're lucky enough to only have to support the newer browsers, go with...
var fauxDocumentFragment = document.createElement("div");
fauxDocumentFragment.innerHTML = str;
[].forEach(fauxDocumentFragment.querySelectorAll("script"), function(script)
script.parentNode.removeChild(script);
});
Upvotes: 2
Reputation: 145368
You can try the following:
str.replace(/<script.*?>.*?<\/script>/m, "");
Upvotes: 1