tags:

views:

48

answers:

4

Hi there,

i need to find inline javascript with php regex. I can find external script, but not inline, can anybody help me?

Here is my pattern for external:

"/<script(.*?)src(.*?)>(.*?)<\/script\s*>/smi"

Any ideas?

A: 

update: and only the <script> nodes without the src attribute set:

"/<script((?:(?!src=).)*?)>(.*?)<\/script>/smix"

(my first look-ahead regex ;))

KARASZI István
Yes, this is for all scripts, but I need the one, without src attribute in opening tag
Simon
try the fixed solution
KARASZI István
cool this almost is correct :) You just need to add ? after first * : "/<script((?:(?!src=).)*?)>(.*?)<\/script>/smix"
Simon
yes, thanks! I fixed it
KARASZI István
A: 
"/<script[^\>]*>(.?)<\/script>/si"
Vadim
that's not good because it'll find only one character long scripts
KARASZI István
Hmmm... Really. Thanks.
Vadim
+2  A: 

You need to find all those cases where inline script can be used (i.e. all listener functions onClick, onBlur, onMouseDown, onMouseUp, on...)).

Thariama
and javascript: urls
Bart van Heukelom
No, i haev HTML doc ant i need to find text like <script type="text/javascript"> var tmp = 3; </script> but not <script language="javascript" type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
Simon
not to mention onload...
no
@no, there are enough ways to run JS. Another way: `<iframe src="http://example.com/evil.htm"></iframe>`. A better solution would be whitelisting using an application like HTML Purifier.
Lekensteyn
yes, there are indeed many many ways to run javascript
Thariama
A: 

If you plan to filter certain HTML (JS for example), use HTML Purifier.

Lekensteyn