473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

RegEx question

Greetings,

I have a troubling issue that I'm not sure how to approach at this point.

Given the HTML tag (any tag will do):

<div id='divSomething' onmouseover='...'>Next we write
onmouseover='alert(message);' ...</div>
I want to write a Regular Expression that only will search the opening div
tag for the "onmouseover" text. My current expression:
(<.*?(ONMOUSEOVER)\s*=.*?>)

will incorrectly detect:
<div id='divSomething'>Next we write onmouseover='alert(message);' ...</div>
Any ideas how I can limit to only the opening tag?
Thanks,
Shawn
Nov 20 '06 #1
6 1146
"Shawn B." wrote:
Given the HTML tag (any tag will do):

<div id='divSomething' onmouseover='...'>Next we write
onmouseover='alert(message);' ...</div>

I want to write a Regular Expression that only will search the opening div
tag for the "onmouseover" text. My current expression:
(<.*?(ONMOUSEOVER)\s*=.*?>)

will incorrectly detect:
<div id='divSomething'>Next we write onmouseover='alert(message);' ...</div>

Any ideas how I can limit to only the opening tag?
The easy way to do this is to replace the the first .* with a [^>]* -
"look for any number of characters that aren't the character",
instead of "look for any number of any character." That is,

(<[^>]*?(ONMOUSEOVER)\s*=.*?>)

--

www.midnightbeach.com/.net
What you need to know.
Nov 20 '06 #2
(?i)(?<=<[\w]+[^<\>=]+)(onmouseover)=(?:["']?([^"'>=]*)["']?)

This regular expression will capture the entire attribute name and value.
The name ("onmouseover" will be in Group 1, and the value in Group 2.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
"Jon Shemitz" <jo*@midnightbeach.comwrote in message
news:45***************@midnightbeach.com...
"Shawn B." wrote:
>Given the HTML tag (any tag will do):

<div id='divSomething' onmouseover='...'>Next we write
onmouseover='alert(message);' ...</div>

I want to write a Regular Expression that only will search the opening
div
tag for the "onmouseover" text. My current expression:
(<.*?(ONMOUSEOVER)\s*=.*?>)

will incorrectly detect:
<div id='divSomething'>Next we write onmouseover='alert(message);'
...</div>

Any ideas how I can limit to only the opening tag?

The easy way to do this is to replace the the first .* with a [^>]* -
"look for any number of characters that aren't the character",
instead of "look for any number of any character." That is,

(<[^>]*?(ONMOUSEOVER)\s*=.*?>)

--

www.midnightbeach.com/.net
What you need to know.

Nov 21 '06 #3
(?i)(?<=<[\w]+[^<\>=]+)(onclick)=(?:["']?([^"'>=]*)["']?)
>
This regular expression will capture the entire attribute name and value.
The name ("onmouseover" will be in Group 1, and the value in Group 2.
Using Regulator, the above expression does not work on the following test
cases:

<SCRIPT NAME=Happy VALUE='happier' ATTR="happiest"
onClick='dosomething();'>CONENT</SCRIPT>
<SCRIPT name=ha onclick = 'asdf'>asdf</SCRIPT>
<tag>var x = asdf.onclick="";</tag>

It should detect #1 and #2 but ignore #3
Thanks,
Shawn
Nov 29 '06 #4
I don't have the original question you asked, and I'm not sure you specified
what the rules should be. Neither do I have the original Regular Expression
I posted for you. The one you posted is modified. So, I can't tell you what
rules I assumed for those which were not provided, nor can I tell you
whether the change you made to the regular expression has anything to do
with it.

Therefore, I went back into my personal library, and found a Regular
Expression I once created for another project, which identifies all
attribute names and values (in 2 groups) in a block of HTML text. The
original was this, to capture *all* attribute names and values:

(?i)\s+(?:(\w+)=(?:["']?([^"'>=]*)["']?)(?=\s|/?>)|\s*(?=\s|/?>))

The first group is defined by the sequence: (\w+) (any sequence of one or
more alpha-numeric characters).

I replaced that with the following:

(?i)\s+(?:(onclick)=(?:["']?([^"'>=]*)["']?)(?=\s|/?>)|\s*(?=\s|/?>))

This will only capture attributes with a name of "onclick"
(case-insensitive)

Upon testing it with your script sample below, it correctly identified only
ONE of the attributes, the first one. The reason it didn't identify the
second one you said that it should is that the second one is not correct
syntactically. In HTML, the '=' character in an attribute may not be
preceded or followed by any spaces.

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

Parabola is a mate of plane.
"Shawn B." <le****@html.comwrote in message
news:uZ**************@TK2MSFTNGP06.phx.gbl...
>(?i)(?<=<[\w]+[^<\>=]+)(onclick)=(?:["']?([^"'>=]*)["']?)

This regular expression will capture the entire attribute name and value.
The name ("onmouseover" will be in Group 1, and the value in Group 2.

Using Regulator, the above expression does not work on the following test
cases:

<SCRIPT NAME=Happy VALUE='happier' ATTR="happiest"
onClick='dosomething();'>CONENT</SCRIPT>
<SCRIPT name=ha onclick = 'asdf'>asdf</SCRIPT>
<tag>var x = asdf.onclick="";</tag>

It should detect #1 and #2 but ignore #3
Thanks,
Shawn


Nov 29 '06 #5
Kevin, thanks for your reply. Actually, I'm trying to look for cross site
scripting vulnerabilities on input fields. While the '=' preceded or
superceded by a space isn't valid html, the browser (IE) will still render
it and treat it the same, and it is a perfectly valid detection evasion
technique. The expression you provided actually still allows a few false
positives to go through on our system but I did find an express that works
flawlessly:

(<[^>]*?(ONMOUSEOVER)\s*=.*?>)

This expression catches every one of our known vulnerabilities and does not
catch any of our known false positives. However, I'll take a closer look at
your expression and figure out if we can adapt it to other parts of our
scanning engine.
Thanks,
Shawn

"Kevin Spencer" <sp**@uce.govwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I don't have the original question you asked, and I'm not sure you
specified what the rules should be. Neither do I have the original Regular
Expression I posted for you. The one you posted is modified. So, I can't
tell you what rules I assumed for those which were not provided, nor can I
tell you whether the change you made to the regular expression has anything
to do with it.

Therefore, I went back into my personal library, and found a Regular
Expression I once created for another project, which identifies all
attribute names and values (in 2 groups) in a block of HTML text. The
original was this, to capture *all* attribute names and values:

(?i)\s+(?:(\w+)=(?:["']?([^"'>=]*)["']?)(?=\s|/?>)|\s*(?=\s|/?>))

The first group is defined by the sequence: (\w+) (any sequence of one or
more alpha-numeric characters).

I replaced that with the following:

(?i)\s+(?:(onclick)=(?:["']?([^"'>=]*)["']?)(?=\s|/?>)|\s*(?=\s|/?>))

This will only capture attributes with a name of "onclick"
(case-insensitive)

Upon testing it with your script sample below, it correctly identified
only ONE of the attributes, the first one. The reason it didn't identify
the second one you said that it should is that the second one is not
correct syntactically. In HTML, the '=' character in an attribute may not
be preceded or followed by any spaces.

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

Parabola is a mate of plane.
"Shawn B." <le****@html.comwrote in message
news:uZ**************@TK2MSFTNGP06.phx.gbl...
>>(?i)(?<=<[\w]+[^<\>=]+)(onclick)=(?:["']?([^"'>=]*)["']?)

This regular expression will capture the entire attribute name and
value. The name ("onmouseover" will be in Group 1, and the value in
Group 2.

Using Regulator, the above expression does not work on the following test
cases:

<SCRIPT NAME=Happy VALUE='happier' ATTR="happiest"
onClick='dosomething();'>CONENT</SCRIPT>
<SCRIPT name=ha onclick = 'asdf'>asdf</SCRIPT>
<tag>var x = asdf.onclick="";</tag>

It should detect #1 and #2 but ignore #3
Thanks,
Shawn



Nov 29 '06 #6
My pleasure, Shawn. As always, figuring out the business rules is the
hardest part!

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

Parabola is a mate of plane.
"Shawn B." <le****@html.comwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
Kevin, thanks for your reply. Actually, I'm trying to look for cross site
scripting vulnerabilities on input fields. While the '=' preceded or
superceded by a space isn't valid html, the browser (IE) will still render
it and treat it the same, and it is a perfectly valid detection evasion
technique. The expression you provided actually still allows a few false
positives to go through on our system but I did find an express that works
flawlessly:

(<[^>]*?(ONMOUSEOVER)\s*=.*?>)

This expression catches every one of our known vulnerabilities and does
not catch any of our known false positives. However, I'll take a closer
look at your expression and figure out if we can adapt it to other parts
of our scanning engine.
Thanks,
Shawn

"Kevin Spencer" <sp**@uce.govwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>>I don't have the original question you asked, and I'm not sure you
specified what the rules should be. Neither do I have the original Regular
Expression I posted for you. The one you posted is modified. So, I can't
tell you what rules I assumed for those which were not provided, nor can I
tell you whether the change you made to the regular expression has
anything to do with it.

Therefore, I went back into my personal library, and found a Regular
Expression I once created for another project, which identifies all
attribute names and values (in 2 groups) in a block of HTML text. The
original was this, to capture *all* attribute names and values:

(?i)\s+(?:(\w+)=(?:["']?([^"'>=]*)["']?)(?=\s|/?>)|\s*(?=\s|/?>))

The first group is defined by the sequence: (\w+) (any sequence of one or
more alpha-numeric characters).

I replaced that with the following:

(?i)\s+(?:(onclick)=(?:["']?([^"'>=]*)["']?)(?=\s|/?>)|\s*(?=\s|/?>))

This will only capture attributes with a name of "onclick"
(case-insensitive)

Upon testing it with your script sample below, it correctly identified
only ONE of the attributes, the first one. The reason it didn't identify
the second one you said that it should is that the second one is not
correct syntactically. In HTML, the '=' character in an attribute may not
be preceded or followed by any spaces.

--
HTH,

Kevin Spencer
Microsoft MVP
Logostician
http://unclechutney.blogspot.com

Parabola is a mate of plane.
"Shawn B." <le****@html.comwrote in message
news:uZ**************@TK2MSFTNGP06.phx.gbl...
>>>(?i)(?<=<[\w]+[^<\>=]+)(onclick)=(?:["']?([^"'>=]*)["']?)

This regular expression will capture the entire attribute name and
value. The name ("onmouseover" will be in Group 1, and the value in
Group 2.
Using Regulator, the above expression does not work on the following
test cases:

<SCRIPT NAME=Happy VALUE='happier' ATTR="happiest"
onClick='dosomething();'>CONENT</SCRIPT>
<SCRIPT name=ha onclick = 'asdf'>asdf</SCRIPT>
<tag>var x = asdf.onclick="";</tag>

It should detect #1 and #2 but ignore #3
Thanks,
Shawn




Nov 29 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.