You can try several different solutions depending on what your actualy need:
Lets take this tag as an example :<source onerror="alert(1)">
- geting only attribute name and value (matches exclude = and "):
/<{1}\w+[\w\s\'\"\=]*(on[^=-\s]+)=["']([\S\w\d]*|[\S\w\d ]*)["']>{1}/gmi
this will return array looking like this:
array (size=2)
0 => string 'onerror'
1 => string 'alert(1)'
Demo with multiple tests
- geting attribute with value (matches include = and "):
/<{1}\w+[\w\s\'\"\=]*((on[^=-\s]+)=["']([\S\w\d]*|[\S\w\d ]*)["'])>{1}/gmi
this will return array looking like this:
array (size=3)
0 => string 'onerror="alert(1)"'
1 => string 'onerror'
2 => string 'alert(1)'
Demo with multiple tests
- geting the entire tag:
/(<{1}\w+[\w\s\'\"\=]*(on[^=-\s]+)=["']([\S\w\d]*|[\S\w\d ]*)["']>{1})/gmi
this will return array looking like this:
array (size=3)
0 => string '<source onerror="alert(1)">'
1 => string 'onerror'
2 => string 'alert(1)'
Demo with multiple tests
- geting all of the above:
/(<{1}\w+[\w\s\'\"\=]*((on[^=-\s]+)=["']([\S\w\d]*|[\S\w\d ]*)["'])>{1})/gmi
this will return array looking like this:
array (size=4)
0 => string '<source onerror="alert(1)">'
1 => string 'onerror="alert(1)"'
2 => string 'onerror'
3 => string 'alert(1)'
Demo with multiple tests
EDIT: This is my final edit of this answer. I will not continue to expand it since RegEx is a "not recomended" way to parse HTML code.