$_ = qq~
<div onMouseOver="this.style.background = 'white';"
onMouseOut=this.style.background = 'black'
onClick="console.log('test');">
~;
s#(?:onclick|onmouseover|onmouseout)=("|')?.*?\1##gsi; <div this.style.background= 'white';"
this.style.background='black'
console.log('test');"> i think this will require two substitutions - one for when the attribute value is enclosed in quotes and another otherwise.
# I discovered that this.style.background = 'white' (with whitespaces) doesn't
# work anyway, so I'm assuming that the value does NOT have a whitespace. But
# a line COULD end with a whitespace and a \n, so I'm allowing \s*. It's also very
# likely that the content will have more than one element like this, so I'm testing
# with 2 DIV elements
$_ = qq~
<div onMouseOver=this.style.background='white'
onMouseOut = this.style.background="black"
onClick=
"console.log('test');">
<div onClick =console.log('test');>
~;
# count how many = are in the string as a safety next for WHILE()
$count = () = /=/g;
$x = $y = 0;
# I have to do another loop for EVERY regex inside of an element, or it will
# miss an element with 2 or more attributes to match
while (
$x <= $count &&
m#<([^>]*?)=\s*([\w\d].*?)[\s>]#gsi) {
$attrName = $1;
$attrVal = $2;
$patt = $1 . $2;
$qte = ($attrVal =~ /"/) ? "'" : '"';
$repl = $attrName . $qte . $attrVal . $qte;
s#\Q$patt\E#$repl#gsi;
$x++;
}
while (
$y <= $count &&
m#<(?:[^>]*?)((?:onclick|onmouseover|onmouseout)\s*=\s*("|').*?\2\s*)(?:[^>]*?>)#gsi) {
$patt = $1;
s#\Q$patt\E##gsi;
$y++;
}
print; I've started writing a regex that will enforce a " or ' to surround the value, and then future regexes won't have to worry about itYup. I've found that “two steps forward, one step back” often ends up being the most straightforward and painless solution. Spend hours tearing out your hair to arrive at a one-line approach, or just dash off two lines and be done with it.