I need to validate the incoming string for text <script
.
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>
tag or not.
I ended up writing something like: <* ?script.* ?>
But the challenge is, Incoming string may contain script in 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";
Hence the regular expression should check for starting <
tag and then it should check for script
.
The regex based solution I would recommend is the following:
This regex will correctly identify and remove script tags in the following strings:
Bonus, it will not match on any of the following invalid script strings:
@bodhizero’s accepted answer of
<[^>]*script
incorrectly returnstrue
under the following conditions:Here is an excellent resource for building and testing regular expressions.
Try this:
A negated character class comes in handy here.
I think this one definitely works for me.