I'm trying to get a list of all inline event tags from an HTML <body>
string, how would I be able to do this?
Example:
<a onclick='foo()'></a>
I'd want to extract onclick='foo()'
.
Is it possible with REGEX or any other alternatives?
I'm trying to get a list of all inline event tags from an HTML <body>
string, how would I be able to do this?
Example:
<a onclick='foo()'></a>
I'd want to extract onclick='foo()'
.
Is it possible with REGEX or any other alternatives?
Here's one. The event-thing will be group 1:
<\w+[^>]+(on[a-z]+=["'][^"']+["'])[^>]*>
You should let the browser do the parsing, for example like this:
var doc = document.implementation.createHTMLDocument('');
doc.documentElement.innerHTML = '<body onload="alert(1)"></body>'; // your string here
Then get the on*
attributes using DOM methods:
var attributes = Array.prototype.slice.call(doc.body.attributes);
for (var i = 0; i < attributes.length; i++) {
if (/^on/.test(attributes[i].name)) {
console.log(attributes[i].name, attributes[i].value);
}
}