My [php executed] regex is terrible and I'm struggling with trying to isolate javascript scripting within HTML blocks. I have the following regex that works partially, but it's run into a problem if there's the word "on" in the text (as opposed to in a < tag >).
$regex = "/<script.*?>.*?<\/script.*?>(*SKIP)(*F)|((\\bon(.*?=)(.*?))(\'|\")(.*?)(\\5))/ism";
$html = preg_replace_callback($regex,
function ($matches) {
$mJS = $matches[2] . $matches[5] . myFunction($matches[6]) . $matches[5];
return $mJS;
},
$html);
I think the issue is that the \bon.... part needs to be qualified to be inside a < tag > before being considered, but I just don't know how.
Running the following test...
$html= "<div id='content' onClick='abc()'>Lorem On='abc' ipsum on to</div>
<input id='a' type='range'>
<input id='b' type='range'>
<script>abc();</script>";
Returns...
<div id='content' onClick='****abc()****'>Lorem On='****abc****' ipsum on to</div>
<input id='****a****' type='range'>
<input id='b' type='range'>
<script>abc();</script>
but I wanted...
<div id='content' onClick='****abc()****'>Lorem On='abc' ipsum on to</div>
<input id='a' type='range'>
<input id='b' type='range'>
<script>****abc();****</script>
I have a sandbox running this if you want to have a play: https://onlinephp.io/c/a43b1
Does anyone have any suggestions?