How to find "<script>" tag from the string with JAVASCRIPT/ regular expression
The regex based solution I would recommend is the following:
Regex rMatch = new Regex(@"<script[^>]*>(.*?)</script[^>]*>", RegexOptions.IgnoreCase & RegexOptions.Singleline);
myString = rMatch.Replace(myString, "");
This regex will correctly identify and remove script tags in the following strings:
<script></script>
<script>something...</script>
something...<ScRiPt>something...</scripT>something...
something...<ScRiPt something...="something...">something...</scripT something...>something...
Bonus, it will not match on any of the following invalid script strings:
< script></script>
<javascript>something...</javascript>
Try this:
/(<|%3C)script[\s\S]*?(>|%3E)[\s\S]*?(<|%3C)(\/|%2F)script[\s\S]*?(>|%3E)/gi
Use:
/<script[\s\S]*?>[\s\S]*?<\/script>/gi
@bodhizero’s accepted answer of <[^>]*script
incorrectly returns true
under the following conditions:
// Not a proper script tag.
const a = "This is a simple < script> string";
// Space added before "img", otherwise the entire tag fails to render here.
const a = "This is a simple < img src='//example.com/script.jpg'> string";
// Picks up "nonsense code" just because a '<' character happens to precede a 'script' string somewhere along the way.
const a = "This is a simple for(i=0;i<5;i++){alert('script')} string";
Here is an excellent resource for building and testing regular expressions.