tags:

views:

175

answers:

7

Hi, I have a big html file (87000+ lines) and want to delete all occurrences of onclick from all elements. Two examples of what I want to catch are:

 1. onclick="fnSelectNode('name2',2, true);"
 2. onclick="fnExpandNode('img3','node3','/include/format_DB.asp?Index2=3');"

The problem is that both function names and parameters passed to them are not the same. So I need a Regular expression that matches onclick= + anything + );"

And I need one that works in Notepad++

Thanks for helping ;-)

A: 

This regular expression will fail if you have a " or ' character included within quotes escaped by a \. Other than that, this should do it.

(onclick="[^"]+")|(onclick='[^"]+')
Dan Herbert
A: 

Could

onclick=\".+;\"

Work?

Isaac Hodes
A: 

Not familiar with notepad++, but what I use in vim is:

onclick="[^"]+"

Of course this depends on there being double quotes around the onclick in every case...

EMiller
you didn't try this one did you... it would turn this: `<a onclick="something" href="blah">` into this: `<a >`
nickf
This regex will be extra greedy, so it will match things like this: `onclick="fnSelectNode('name2',2, true);" href="#"` in their entirety.
Dan Herbert
True, it is greedy, but fortunately in my case the onclick attribute was last in all instances. So I got the replacement I looked for. Thank anybody for your replies.
Majid
You could use the non-greedy `.*?` operator instead of `.*` if the regex engine in NP++ supports it.
Amber
I have used the .* version of that regex quite a bit - but, like Majid, the onclick was always last. But you're right, it's not really correct.
EMiller
Dav, I find regex hard to understand and just fail to comprehend how .*? could stop the greediness.
Majid
Majid: `*?` is the non-greedy version of `*` in most languages. (It's not `*` + `?`, it's a single specifier.)
Amber
s/languages/regex engines
Amber
+1  A: 

onclick="[^"]+" works for me, for that 2 strings.

S.Mark
A: 
 onclick=\".*\);\"
cxfx
+1  A: 

If you want to go with a regex:

/onclick=".*?"/

You could also use something which is DOM-aware, such as a HTML/XML parser, or even just load up jQuery:

$("[onclick]").removeAttr("onclick");

... and then copy the body HTML into a new file.

nickf
Yay, jQuery is big deal and always a help.
Majid
A: 

This regex should do the trick.

(\s+onclick=(?:"[^"]+")|(?:'[^']+'))
nickyt