" tag from a string using JAVASCRIPT / regular expression I need to check the input string for text

How to find the "<script>" tag from a string using JAVASCRIPT / regular expression

I need to check the input string for text <script .

Example:
 string a = "This is a simple <script> string"; 

Now I need to write a regular expression that will tell me whether this string contains a <script> or not.

In the end, I wrote something like: <* ?script.* ?>

But the problem is, the Incoming line may contain the script in the following ways:

 string a = "This is a simple <script> string"; string a = "This is a simple < script> string"; string a = "This is a simple <javascript></javascript> string"; string a = "This is a simple <script type=text/javascript> string"; 

Therefore, the regular expression must check the start of the < tag, and then it must be checked against the script .

+8
source share
5 answers

Not applicable here.

 <[^>]*script 
+1
source

Using:

 /<script[\s\S]*?>[\s\S]*?<\/script>/gi 

@bodhizeros answer <[^>]*script incorrectly returns true under the following conditions:

 // Not a proper script tag. string a = "This is a simple < script> string"; // Space added before "img", otherwise the entire tag fails to render here. string 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. string a = "This is a simple for(i=0;i<5;i++){alert('script')} string"; 

Here is a great resource for creating and testing regular expressions .

+25
source

I would recommend a regex based solution:

 Regex rMatch = new Regex(@"<script[^>]*>(.*?)</script[^>]*>", RegexOptions.IgnoreCase & RegexOptions.Singleline); myString = rMatch.Replace(myString, ""); 

This regular expression will correctly identify and remove script tags in the following lines:

 <script></script> <script>something...</script> something...<ScRiPt>something...</scripT>something... something...<ScRiPt something...="something...">something...</scripT something...>something... 

Bonus, it will not match any of the following invalid script lines:

 < script></script> <javascript>something...</javascript> 
+3
source

Try the following:

 /(<|%3C)script[\s\S]*?(>|%3E)[\s\S]*?(<|%3C)(\/|%2F)script[\s\S]*?(>|%3E)/gi 
+3
source

I think this definitely works for me.

 var regexp = /<script+.*>+.*<\/script>/g; 
0
source

Source: https://habr.com/ru/post/945149/


All Articles