Replacing href attributes with onClick events with Regex

I recently had the need to override all of my links on a Sencha Touch app with an onClick event. I needed to be able to open the links using the ChildBrowser plugin for PhoneGap. The problem was that I needed to grab the links before I rendered the content to the page and apply the onClick event instead of the href.

Essentially, I wanted to turn this:

Into this:

So how did I do it? I wrote a function to parse the string using Regex before I rendered it to the page. I thought that the whole operation my be fairly common, so I Googled how to do it, and to my surprise, there was literally nothing on the internet about how to do it, so I naturally thought it was worth posting.

I’m still pretty new to Regex, so there may well be a better way to do this. If you know of one, don’t hesitate to put me in my place. Enjoy!