Skip to content
Advertisement

regex to isolate javascript using preg_replace_callback

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 >).

JavaScript

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…

JavaScript

Returns…

JavaScript

but I wanted…

JavaScript

I have a sandbox running this if you want to have a play: https://onlinephp.io/c/a43b1

Does anyone have any suggestions?

Advertisement

Answer

With help from Bobble Bubble, I’ve been able to get this working…

JavaScript

See https://onlinephp.io/ for a running executable.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement