replace href with angular regular expression
<p>I'm looking for a filter to replace "href" in a TextEntity of an API. The "text" can  contains 3 different kind of URLs. After the replacement I want to open the corrected URLs in a new separate window. </p>

<p>I receive from the textvalue the following information:</p>

<pre><code>1. &lt;a href="http://someurl.tdl"&gt;link&lt;/a&gt; - this is an example for a all kind of external links. Can be mysite.com/mypage.html or any other valid url. Like everything with a http://, https://, ftp:// in the startof the url.
2. &lt;a href="singpage.html"&gt;internal page&lt;/a&gt; - Can includes all other files. like mypdf.pdf or mydoc.doc or other stuff, but without http://mydomain.tdl
3. &lt;a href="mailto: mymail@domain.tdl"&gt;mymail@domain.tdl&lt;/a&gt;
</code></pre>

<p>I tried something but it doesn't work.</p>

<pre><code>.filter('parseText', function ($sce, $sanitize) {
var mydomain = 'http://www.mydomain.tdl';
return function (text) {
    var newStringUrlReplace = $sanitize(text).replace('href="','href="'+mydomain);
    var regex = /href="([\S]+)"/g;
    var newString = newStringUrlReplace.replace(regex, "class=\"externalURL\" onClick=\"cordova.InAppBrowser.open('$1', '_blank', 'location=yes')\"");
    return $sce.trustAsHtml(newString);
    }
});
</code></pre>

<p>I need this output the "text" ran went through the filter:</p>

<pre><code>1. &lt;a href="http://someurl.tdl" class="externalURL" onClick="cordova.InAppBrowser.open('http://someurl.tdl', '_blank', 'location=yes')"&gt;link&lt;/a&gt;
2. &lt;a href="http://www.mydomain.tdl/singpage.html" onClick="cordova.InAppBrowser.open('http://www.mydomain.tdl/singpage.html', '_blank', 'location=yes')"&gt;internal page&lt;/a&gt;
3. &lt;a href="mailto: mymail@domain.tdl"&gt;mymail@domain.tdl&lt;/a&gt;
</code></pre>

<p>To make it easier to understand: </p>

<p>I need a function which turns this types of URLs.</p>

<pre><code>&lt;a href="http://someurl.tdl/whichcanincludeanything.html?bar=foo"&gt;URL TO A  EXTERNAL PAGE&lt;/a&gt;

&lt;a href="singpage.html"&gt;internal page of the CMS&lt;/a&gt; 
</code></pre>

<p>into </p>

<pre><code>&lt;a href="http://someurl.tdl/whichcanincludeanything.html?bar=foo" class="externalURL" onClick="cordova.InAppBrowser.open('http://someurl.tdl/whichcanincludeanything.html?bar=foo', '_blank', 'location=yes')"&gt;URL TO A  EXTERNAL PAGE&lt;/a&gt;

&lt;a href="http://www.mydomain.tdl/singpage.html" onClick="cordova.InAppBrowser.open('http://www.mydomain.tdl/singpage.html', '_blank', 'location=yes')"&gt;internal page&lt;/a&gt;
</code></pre>