Reputation: 505
I'm trying to run a regex to to do this:
Replace all instances of an apk file in a document:
<a href="http://download.mydomain.com/files/androidfile.apk">
With:
<a target="_blank" href="http://mydomain.com?f=http://download.mydomain.com/files/androidfile.apk">
With this JQUERY code:
$('body').contents().filter(function() {
return this.nodeType === 3;
}).each(function() {
$(this).replaceWith($(this).text().replace(/<a href~/^.*\.(apk)$/i, 'http://mydomain.com?f=">$1</a>'));
});
So essentially, remove the code before download.mydomain.com/files/androidfile.apk
, that is <a href="http://
and replace with <a target="_blank" href="http://mydomain.com?f=http://
. There might be several of this in a page so I need a solution that might match and replace all.
The Regex and replacing doesn't seem to work, what might be a solution to this.
So in summary, I want to be able to do this in JQuery:
<html>
<a href="http://download.domain.com/files/androidfile1.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile2.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile3.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile4.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile5.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
</html>
To this:
<html>
<a href="http://mydomain2.com?f=http://download.domain.com/files/androidfile1.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://mydomain2.com?f=http://download.domain.com/files/androidfile2.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://mydomain2.com?f=http://download.domain.com/files/androidfile3.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://mydomain2.com?f=http://download.domain.com/files/androidfile4.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://mydomain2.com?f=http://download.domain.com/files/androidfile5.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
</html>
Upvotes: 0
Views: 204
Reputation: 8763
The code below should do the trick. Rather than use regex, I used the ends with selector. This should serve the same purpose, and execute quicker. Instead of rewriting the tag completely, I just modified the attributes using jQuery.attr(). A live example is here.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('a[href$=apk]').each(function()
{
var mydomain = "http://mydomain2.com?f=";
var $this = $(this);
$this.attr("href", mydomain+$this.attr("href"));
$this.attr("target", "_blank");
});
});
</script>
</head>
<body>
<a href="http://download.domain.com/files/androidfile1.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile2.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile3.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile4.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
<a href="http://download.domain.com/files/androidfile5.apk"><img src="theme/download.gif" border=0 alt="download" height="120px" width="130px"></a><br/>
</body>
</html>
The resulting output is:
<a target="_blank" href="http://mydomain2.com?f=http://download.domain.com/files/androidfile1.apk"><img src="theme/download.gif" alt="download" height="120px" border="0" width="130px"></a><br>
<a target="_blank" href="http://mydomain2.com?f=http://download.domain.com/files/androidfile2.apk"><img src="theme/download.gif" alt="download" height="120px" border="0" width="130px"></a><br>
<a target="_blank" href="http://mydomain2.com?f=http://download.domain.com/files/androidfile3.apk"><img src="theme/download.gif" alt="download" height="120px" border="0" width="130px"></a><br>
<a target="_blank" href="http://mydomain2.com?f=http://download.domain.com/files/androidfile4.apk"><img src="theme/download.gif" alt="download" height="120px" border="0" width="130px"></a><br>
<a target="_blank" href="http://mydomain2.com?f=http://download.domain.com/files/androidfile5.apk"><img src="theme/download.gif" alt="download" height="120px" border="0" width="130px"></a><br>
Upvotes: 1
Reputation: 144729
What you are doing is overkill, although it won't work, why selecting all the elements on the page? if you want to select a
elements why not selecting them using $('a')
? For adding a string to the href
attributes, you can use attr
method.
$('a').filter(function() {
return this.href.indexOf('apk')
}).attr('href', function(i, href) {
return 'http://mydomain.com?f=' + href
}).attr('target', '_blank');
Upvotes: 1